简体   繁体   中英

Django specific error on running code in settings.py

I am trying to create an html file when the django project is started, by including code in the project/settings.py as follows:

def Brander():
    import configparser
    config = configparser.ConfigParser()
    config.read('settings.ini')
    version = config['PROJECT']['version']
    APPNAME = config['BRANDING']['appname']
    APPCOMPANY = config['BRANDING']['appcompany']
    APPCOMPANYLINK = config['BRANDING']['appcompanysite']
    APPLINK = config['BRANDING']['appsite']
    from django.contrib.staticfiles import finders
    filen = finders.find('clinic/brandedfooter.html')
    f = open(filen, "w")
    s = f"""
    <div class="col-lg-8 col-md-8 d-none d-md-block d-lg-block">
        <span class="text-muted float-right"><i>My OP and IP Clinic - <a href="{APPLINK}">{APPCOMPANY} by </a><a href="{APPCOMPANYLINK}">{APPCOMPANY}</a></i></span>
    </div>
    """
    f.write(s)

My project/settings.ini contains:

[PROJECT]
version = 0.0.1

[BRANDING]
appname = MyOPIP
appcompany = Droidzone
appcompanysite = https://droidzone.in
appsite = https://myopip.com

When the above code is run as standalone python script, everything works fine, and the html file is generated. However, when this is executed as part of manage.py runserver , I get the following error:

joel@hp:~/myappointments$ ./manage.py runserver
Traceback (most recent call last):
File "./manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
File "/home/joel/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
File "/home/joel/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 325, in execute
    settings.INSTALLED_APPS
File "/home/joel/.local/lib/python3.6/site-packages/django/conf/__init__.py", line 57, in __getattr__
    self._setup(name)
File "/home/joel/.local/lib/python3.6/site-packages/django/conf/__init__.py", line 44, in _setup
    self._wrapped = Settings(settings_module)
File "/home/joel/.local/lib/python3.6/site-packages/django/conf/__init__.py", line 107, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/joel/myappointments/myappointments/settings.py", line 258, in <module>
    Brander()
File "/home/joel/myappointments/myappointments/settings.py", line 16, in Brander
    version = config['PROJECT']['version']
File "/usr/lib/python3.6/configparser.py", line 959, in __getitem__
    raise KeyError(key)
KeyError: 'PROJECT'

I'm unable to understand why this error occurs only on starting the code in django.

You can use Django Constance for defining the settings. For example:

CONSTANCE_CONFIG = {
    'VERSION': ('0.0.1', 'Version'),
}

Then add 'constance.context_processors.config' in you context processors like the documentation mentioned. Then use it in template:

{{ config.VERSION }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM