[Fixed]-How to store a dynamic site-wide variable

2👍

For user specific variables, use session.

For global constants (not variables!), use settings.py.

For global variables, consider to store it in database so it can be multithreading & multiprocess safe.

0👍

I looked around and saw different approaches,but one that doesn’t compromise the DRY philosophy the most for me is registering a tag in your project then input it in the base template.Its neater See here https://stackoverflow.com/a/21062774/6629594 for an example

👤A.Lang

0👍

Storage can take any number of places, I put mine in a stats model in the db so you get all the goodness of that (and make it easy to access in views).

I then have a context processor written as so:

#context_processors.py:
def my_custom_context_processor(request):
    return {'custom_context_variable1':'foo','custom_context_variable2':'bar'}

Add this to your context processors in settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
...
"my_app.context_processors.ny_custom_context_processor", 
)

Provided you use render() to render your templates you can then you can just use:

{{ custom_context_variable1 }}

to return ‘foo’ in your template. Obviously returning strings is for example only, you can use anything you like so long as your context processor returns a dict.

-1👍

you can also try using php pages.

Then acces the variable on each page with an include ‘file containing the var.php’ on every page.

None of this will be visible in the source html as it is only processed on the server side.

If you you would like to try this, mail me and I will send you some sample code.

Leave a comment