• pythoncharm 中解决启动server时出现 “django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured”的错误


    背景介绍

    最近,尝试着用pythoncharm 这个All-star IDE来搞一搞Django,于是乎,下载专业版,PJ等等一系列操作之后,终于得偿所愿。可以开工了。

    错误

    在园子里找了一篇初学者的教程,按照教程上面的指导,一步一步往下走,一路上都很顺利。但是到了启动django server的时候出现了意外。

    错误信息如下:

    /home/ct/PycharmProjects/mysite/venv/bin/python /home/ct/PycharmProjects/mysite/manage.py runserver 127.0.0.1:8000
    Traceback (most recent call last):
      File "/home/ct/PycharmProjects/mysite/manage.py", line 22, in <module>
        execute_from_command_line(sys.argv)
      File "/home/ct/PycharmProjects/mysite/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
        utility.execute()
      File "/home/ct/PycharmProjects/mysite/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/home/ct/PycharmProjects/mysite/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
        self.execute(*args, **cmd_options)
      File "/home/ct/PycharmProjects/mysite/venv/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 61, in execute
        super(Command, self).execute(*args, **options)
      File "/home/ct/PycharmProjects/mysite/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
        output = self.handle(*args, **options)
      File "/home/ct/PycharmProjects/mysite/venv/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 72, in handle
        if not settings.DEBUG and not settings.ALLOWED_HOSTS:
      File "/home/ct/PycharmProjects/mysite/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 56, in __getattr__
        self._setup(name)
      File "/home/ct/PycharmProjects/mysite/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 39, in _setup
        % (desc, ENVIRONMENT_VARIABLE))
    
    django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
    
    Process finished with exit code 1

    分析原因

    最初,分析原因猜测是Django的安装出了问题; 但是,使用 terminal来启动django, 一切正常啊。

    ct@ubuntu:~/PycharmProjects/mysite$ python manage.py runserver 127.0.0.1:8000
    Performing system checks...
    
    System check identified no issues (0 silenced).
    
    You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
    Run 'python manage.py migrate' to apply them.
    
    April 05, 2018 - 13:53:41
    Django version 1.11, using settings 'mysite.settings'

    Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.

    思考了以后,感觉还是 pythoncharm 里,关于django的设置出了问题。于是打开IDE中的配置,发现缺失了 “DJANGO_SETTINGS_MODULE” 的配置

     

    解决方法

    顺着这个思路,一直找解决方案,终于找到了如何设置django项目的配置。 

    export DJANGO_SETTINGS_MODULE=mysite.settings

    其实也就是添加环境变量罢了,只不过在pythoncharm中,我们需要将环境变量添加到IDE里面。。。

    点击OK后,再次在pythoncharm里运行项目,这次成功啦!!

    参考资料:

    https://stackoverflow.com/questions/26082128/improperlyconfigured-you-must-either-define-the-environment-variable-django-set

    记录一下加深记忆的同时也希望能帮到其他人!! 感谢大家的收看, bye~~

  • 相关阅读:
    队列的实现
    前端的数据结构
    有关es6的模块化
    jQuery---事件的执行顺序
    短网址生成和还原工具使用教程
    【01】HTML_day01_03-HTML常用标签
    JS中变量、作用域的本质,定义及使用方法
    JS 重载父页面
    搞懂Nginx一篇文章就够了
    重复的子字符串
  • 原文地址:https://www.cnblogs.com/atuotuo/p/8724515.html
Copyright © 2020-2023  润新知