My site has few global configurations. For example "smtp-server address", "company address", etc.
Of course I can:
settings.py
and use it in templates and apps;Configuration
) and write all needed fields there.If I use the first way I can't give access for changing these fields in django-admin.
If I use the seconds way it is not a beautiful solution, because everywhere in the code I have to use model_name.objects.get(id=1)
and I need only one instance. Models were created for other tasks.
How can I solve this problem?
This is what I did. Might not be the most optimal solution but works for me.
Create a Configuration model and do all the usual stuff as in your point 2. Create a function (say in configuration.view
) which will pull out and return the configuration values in a dict.
Now in your settings.py
, import your function and set the returned dict to a settings.py
variable:
CONFIG = configuration.view.get_config()
Create a template context processor which will set this CONFIG
dict in the template context.
def init_site_settings(request):
return settings.CONFIG
Add this context processor to your TEMPLATE_CONTEXT_PROCESSORS
Now you are free to use your configuration parameters in templates as {{my_config_key}}
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With