Django specific error on running code in settings.py












0















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.










share|improve this question























  • The relative path to the ini file is incorrect. Try giving the full path or check what the current working directory is and adapt the path accordingly. And IMHO it's not a good idea to add actual code to the settings.

    – Klaus D.
    Nov 22 '18 at 2:57











  • @KlausD. Could you suggest an alternate way to add a version and company name to the footer, which is included several levels below the master template? I dont want to end up passing several variables or an object every time I need to call a view. My app has hundreds of views.

    – Joel G Mathew
    Nov 22 '18 at 3:01






  • 3





    Template inheritance? Custom template tag? Context processor? Middleware? ...

    – Klaus D.
    Nov 22 '18 at 3:18











  • @KlausD. Thanks for the ideas

    – Joel G Mathew
    Nov 22 '18 at 5:29
















0















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.










share|improve this question























  • The relative path to the ini file is incorrect. Try giving the full path or check what the current working directory is and adapt the path accordingly. And IMHO it's not a good idea to add actual code to the settings.

    – Klaus D.
    Nov 22 '18 at 2:57











  • @KlausD. Could you suggest an alternate way to add a version and company name to the footer, which is included several levels below the master template? I dont want to end up passing several variables or an object every time I need to call a view. My app has hundreds of views.

    – Joel G Mathew
    Nov 22 '18 at 3:01






  • 3





    Template inheritance? Custom template tag? Context processor? Middleware? ...

    – Klaus D.
    Nov 22 '18 at 3:18











  • @KlausD. Thanks for the ideas

    – Joel G Mathew
    Nov 22 '18 at 5:29














0












0








0








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.










share|improve this question














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.







python django python-3.x






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 2:52









Joel G MathewJoel G Mathew

2,14792846




2,14792846













  • The relative path to the ini file is incorrect. Try giving the full path or check what the current working directory is and adapt the path accordingly. And IMHO it's not a good idea to add actual code to the settings.

    – Klaus D.
    Nov 22 '18 at 2:57











  • @KlausD. Could you suggest an alternate way to add a version and company name to the footer, which is included several levels below the master template? I dont want to end up passing several variables or an object every time I need to call a view. My app has hundreds of views.

    – Joel G Mathew
    Nov 22 '18 at 3:01






  • 3





    Template inheritance? Custom template tag? Context processor? Middleware? ...

    – Klaus D.
    Nov 22 '18 at 3:18











  • @KlausD. Thanks for the ideas

    – Joel G Mathew
    Nov 22 '18 at 5:29



















  • The relative path to the ini file is incorrect. Try giving the full path or check what the current working directory is and adapt the path accordingly. And IMHO it's not a good idea to add actual code to the settings.

    – Klaus D.
    Nov 22 '18 at 2:57











  • @KlausD. Could you suggest an alternate way to add a version and company name to the footer, which is included several levels below the master template? I dont want to end up passing several variables or an object every time I need to call a view. My app has hundreds of views.

    – Joel G Mathew
    Nov 22 '18 at 3:01






  • 3





    Template inheritance? Custom template tag? Context processor? Middleware? ...

    – Klaus D.
    Nov 22 '18 at 3:18











  • @KlausD. Thanks for the ideas

    – Joel G Mathew
    Nov 22 '18 at 5:29

















The relative path to the ini file is incorrect. Try giving the full path or check what the current working directory is and adapt the path accordingly. And IMHO it's not a good idea to add actual code to the settings.

– Klaus D.
Nov 22 '18 at 2:57





The relative path to the ini file is incorrect. Try giving the full path or check what the current working directory is and adapt the path accordingly. And IMHO it's not a good idea to add actual code to the settings.

– Klaus D.
Nov 22 '18 at 2:57













@KlausD. Could you suggest an alternate way to add a version and company name to the footer, which is included several levels below the master template? I dont want to end up passing several variables or an object every time I need to call a view. My app has hundreds of views.

– Joel G Mathew
Nov 22 '18 at 3:01





@KlausD. Could you suggest an alternate way to add a version and company name to the footer, which is included several levels below the master template? I dont want to end up passing several variables or an object every time I need to call a view. My app has hundreds of views.

– Joel G Mathew
Nov 22 '18 at 3:01




3




3





Template inheritance? Custom template tag? Context processor? Middleware? ...

– Klaus D.
Nov 22 '18 at 3:18





Template inheritance? Custom template tag? Context processor? Middleware? ...

– Klaus D.
Nov 22 '18 at 3:18













@KlausD. Thanks for the ideas

– Joel G Mathew
Nov 22 '18 at 5:29





@KlausD. Thanks for the ideas

– Joel G Mathew
Nov 22 '18 at 5:29












1 Answer
1






active

oldest

votes


















1














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 }}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423222%2fdjango-specific-error-on-running-code-in-settings-py%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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 }}





    share|improve this answer




























      1














      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 }}





      share|improve this answer


























        1












        1








        1







        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 }}





        share|improve this answer













        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 }}






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 3:13









        ruddraruddra

        15.6k32850




        15.6k32850
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423222%2fdjango-specific-error-on-running-code-in-settings-py%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()