0

I'm making a very very reusable CMS in Django. I'm trying not to hardcode anything, so I want a config.py or something for the app itself.

I want to use it in templates (something like {{ config.SITE_NAME }}) and just regular Python code (views, models), etc.

How would I go about doing that?

user1438098
  • 2,239
  • 3
  • 18
  • 14

3 Answers3

2

Django already has the settings.py file and an interface to use the values from there, is that not good enough? Accessing settings in your code is easy:

from django.conf import settings
do_something_with(settings.MY_CONFIG_VARIABLE)

In your template it requires a bit more work, like making a context processor for example. This answer shows you have to make a context processor that exposes values from settings to your template.

Community
  • 1
  • 1
clj
  • 481
  • 2
  • 8
1

settings.py serves this purpose already in django. You can add custom settings to it for your application.

https://docs.djangoproject.com/en/dev/topics/settings/#using-settings-in-python-code

Tritium21
  • 2,845
  • 18
  • 27
-1

When you create a django project, it automatically creates a settings.py file. Put your settings in that file, and then :-

from django.conf import settings  
settings.YOUR_SETTING

also, if you have some settings, that will vary depending upon the machines (eg. production/test servers). Then just add eg. conf/local_settings.py file and put the machine specific settings in that file, and in settings.py just do :-

from conf.local_settings import *

at the end of settings.py Make sure you ignore local_settings.py from your VCS check-in (eg. in case of git add local_settings.py to .gitignore

Siddharth Srivastava
  • 1,059
  • 1
  • 10
  • 22
  • 1
    Instead of the local_settings.py convention, you might recommend things like server specific settings, use of environment variables, or tools like django-configurations. – pydanny Sep 22 '13 at 12:10
  • correct!, my personal recommendation would have been http://zookeeper.apache.org/ , but depends upon need. – Siddharth Srivastava Sep 22 '13 at 12:14