Python django.core.exceptions.ImproperlyConfigured:请求设置LOGGING_CONFIG,但未配置设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28525825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:22:58  来源:igfitidea点击:

django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured

pythondjango

提问by SkillSet12345

I'm trying to run a populate script which I put together from the tango_with_django tutorial (https://github.com/leifos/tango_with_django/blob/master/tango_with_django_project/populate_rango.py) however I'm getting the below traceback and it seems to be related to changes made in Django 1.7? I'd appreciate if someone could explain what I'm doing wrong here.

我正在尝试运行我从 tango_with_django 教程(https://github.com/leifos/tango_with_django/blob/master/tango_with_django_project/populate_rango.py)中汇总的填充脚本,但是我得到了以下回溯和它似乎与 Django 1.7 中所做的更改有关?如果有人能解释我在这里做错了什么,我将不胜感激。

(test_env) C:\Users\WriteCode\test_env\epl>python populate_clubs.py
Traceback (most recent call last):
  File "populate_clubs.py", line 4, in <module>
    django.setup()
  File "c:\Python27\lib\site-packages\django\__init__.py", line 20, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "c:\Python27\lib\site-packages\django\conf\__init__.py", line 46, in __ge
tattr__
    self._setup(name)
  File "c:\Python27\lib\site-packages\django\conf\__init__.py", line 40, in _set
up
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, b
ut settings are not configured. You must either define the environment variable
DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

(test_env) C:\Users\WriteCode\test_env\epl>

My populate_clubs.py script

我的 populate_clubs.py 脚本

import os
import sys
import django
django.setup()


def add_club(name, nickname, manager, established, stadium, active=True):
    c = clubs.objects.get_or_create(name=name, nickname=nickname, manager=manager, established=established, stadium=stadium, active=active)
    return c

def populate():
    add_club(name = "Aston Villa",
        nickname = "Villans",
        manager = "Tim Sherwood",
        established = "1897",
        stadium = "Villa Park"
        )

    # Print out what was added
    for c in clubs.objects.all():
        print "The following has been added to clubs:" + c



# Start execution
if __name__ == '__main__':
    print "Starting club population script..."
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'epl.settings')
    from teams.models import clubs
    populate()

采纳答案by GwynBleidD

Call to django.setupshould go after setting DJANGO_SETTINGS_MODULEenvironment variable. Just move it into your __main__right after os.environ.setdefault.

django.setup应在设置DJANGO_SETTINGS_MODULE环境变量后调用。只需将其移入您的__main__右侧之后os.environ.setdefault

回答by bwangel

You can insert os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")before the django.setup()line.

您可以os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")在该django.setup()行之前插入。

回答by Edward Moffett

If you are getting a similar error after initiating your interaction with Django by running pythonin a terminal, running python manage.py shellin the appropriate directory may solve your issue.

如果通过python在终端中运行python manage.py shell来启动与 Django 的交互后遇到类似的错误,在适当的目录中运行可能会解决您的问题。

回答by All ?? Vаи?тy

For development and debugging, you may use the standalonepython package.

对于开发和调试,您可以使用独立的python 包。

Install with pip

使用 pip 安装

pip install standalone

Use standalone to use Django from the module.

使用 standalone 从模块中使用 Django。

# add the following to the top of the module.
import standalone
standalone.run('mysite.settings') # replace with your settings module.

# your code goes below the configuration
import os
import sys

# ... .. .  whole module

Now you may run the module as a Python Django script.

现在您可以将模块作为 Python Django 脚本运行。

回答by nexla

It happened to me when I used django related import statement in a non-django module.
i.e from index.views import viewthat did raise an error for me.

当我在非 django 模块中使用与 django 相关的导入语句时,它发生在我身上。
from index.views import view这确实给我带来了错误。

回答by cheng

I met the same problem when setting the environment.

我在设置环境时遇到了同样的问题。

(I tried to add the:

(我试图添加:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")

into the "Starting script window",which is in the Django Console or rewrite the .wsgi file, But all of them were failed)

进入Django控制台中的“启动脚本窗口”或重写.wsgi文件,但都失败了)

My final solution: (open the terminal and set the environment manually)

我的最终解决方案:(打开终端并手动设置环境)

Here are the steps:(for mac)

以下是步骤:(对于Mac)

  1. open the terminal and type vim ~/.bash_profile

  2. add below lines:

     PATH=${PATH}:/Users/work/workspace/[virtual_environment]/bin
     PYTHONPATH=${PATH}:/Users/work/PycharmProjects/[project_name]
     DJANGO_SETTINGS_MODULE=[project_name].settings
    
  3. type :wq to save and exit terminal

  4. type source ~/.bash_profile to reload it

  1. 打开终端并输入 vim ~/.bash_profile

  2. 添加以下几行:

     PATH=${PATH}:/Users/work/workspace/[virtual_environment]/bin
     PYTHONPATH=${PATH}:/Users/work/PycharmProjects/[project_name]
     DJANGO_SETTINGS_MODULE=[project_name].settings
    
  3. 输入 :wq 保存并退出终端

  4. 输入 source ~/.bash_profile 重新加载它

it will work when you run python manage.pyshell this time.

这次运行python manage.pyshell时它会起作用。

回答by Flavins

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'epl.settings')

should be above

应该在上面

import django

which is you should call django_settings_module before importing and calling django setup.

这是你应该在导入和调用 django setup 之前调用 django_settings_module 。

Hope this is helpful.

希望这是有帮助的。