First time here? Check out the FAQ!
0

Access settings attribute from template
 

For example, in user_navigation.html template we have following code:

if settings.KARMA_MODE != 'hidden' and settings.BADGES_MODE != 'hidden'

I want to understand how this is been done, lets say I want to introduce a boolean value in the settings.py, how do I access that from my template similar to above technique.

To enter a block of code:

  • enter empty line after your previous text
  • paste or type the code
  • select the code and press the button above
Preview: (hide)
SocialQA's avatar
265
SocialQA
asked 11 years ago

Comments

see more comments

1 Answer

1

If you would like to introduce those values via settings.py, then use the convention of the overrides by the livesettings app. Those settings are not the same as uppercased local variables inside the settings.py file.

LIVESETTINGS_OPTIONS = {
    1: {
        'DB': True,
        'SETTINGS': {
            'KARMA_AND_BADGE_VISIBILITY': {
                'KARMA_MODE': 'private',
                'BADGES_MODE': 'hidden'
            },
         }
    }
}

Where number 1 is site id. Key 'KARMA_AND_BADGE_VISIBILITY' is a section of livesettings - you can see that in the various askbot/conf/*.py files.

In general, I would say this is for the users who do not mind to get their hands on the code. Otherwise it is possible to change them via the /settings/ url.

We might in the future move away from the livesettings app as it has some limitations, and in that case the custom settings specified via the overrides will need to be migrated to the new, hopefully simpler format.

To enter a block of code:

  • enter empty line after your previous text
  • paste or type the code
  • select the code and press the button above
Preview: (hide)
Evgeny's avatar
13.2k
Evgeny
answered 11 years ago
link

Comments

When I search for KARMA_AND_BADGE_VISIBILITY I found karma_adn_badges_visibility.py by that doesn't look like what you have put in the answer.

SocialQA's avatar SocialQA (11 years ago)
1

Yes - open that file and may be it will help you - there inside section with that name is defined. Like I said this is part of the livesettings app which has own conventions of dealing with the settings.

Evgeny's avatar Evgeny (11 years ago)

You have an index with an integer and curly braces where as in this file it has name KARMA_AND_BADGE_VISIBILITY then settings.register with ( ) braces.

SocialQA's avatar SocialQA (11 years ago)
1

That's just Python code. The settings are defined via the function calls and the LIVESETTINGS_OPTIONS is just nested dictionary representing the static overridden values. Once you override them in the settings.py you can't change them via the /settings/ url. The 1: { 'DB': True... part - consider that a preamble to the actual settings. The value of 'SETTINGS' is actually what you want to be editing. We have not made effort to make this part user friendly yet, sorry.

Evgeny's avatar Evgeny (11 years ago)

If I understood correctly what you put in your answer is something that goes into settings.py... if thats the case similarly karama_and_badge should have a attribute in settings.py.. I think i am super confused... wonder if you can show a simple end to end example. Thanks. May be then I can take an approach to clean it up and do an upstream.

SocialQA's avatar SocialQA (11 years ago)
see more comments