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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47700347/
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 18:19:34  来源:igfitidea点击:

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

pythondjangomysql-python

提问by reno_libiii

I want to connect MySQL database to my django project, but it is throwing an error :

我想将 MySQL 数据库连接到我的 django 项目,但它抛出了一个错误:

"django.core.exceptions.ImproperlyConfigured: Requested setting USE_I18N, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings."

“django.core.exceptions.ImproperlyConfigured:请求设置 USE_I18N,但未配置设置。您必须在访问设置之前定义环境变量 DJANGO_SETTINGS_MODULE 或调用 settings.configure()。”

Trace:

痕迹:

 (myenv) LIBINGLADWINs-MacBook-Air:libinrenold$ django-admin dbshell
Traceback (most recent call last):
  File "/Users/libinrenold/Desktop/djangoworks/myenv/bin/django-admin", line 11, in <module>
    sys.exit(execute_from_command_line())
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/core/management/base.py", line 322, in execute
    saved_locale = translation.get_language()
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/utils/translation/__init__.py", line 195, in get_language
    return _trans.get_language()
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/utils/translation/__init__.py", line 59, in __getattr__
    if settings.USE_I18N:
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
    self._setup(name)
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/conf/__init__.py", line 39, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting USE_I18N, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

settings.py.

设置.py

  DATABASES = {
        'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test',
        'USER': 'user',
        'PASSWORD': 'root',
        'HOST':'',
        'PORT': '',
        }
     }

回答by raratiru

You must define the relevant variable to show where you settings.pyfile lives:

您必须定义相关变量以显示settings.py文件所在的位置:

export DJANGO_SETTINGS_MODULE=mysite.settings

This is the relevant docsentry:

这是相关的文档条目:

When you use Django, you have to tell it which settings you're using. Do this by using an environment variable, DJANGO_SETTINGS_MODULE.

The value of DJANGO_SETTINGS_MODULE should be in Python path syntax, e.g. mysite.settings. Note that the settings module should be on the Python import search path.

当您使用 Django 时,您必须告诉它您正在使用哪些设置。通过使用环境变量 DJANGO_SETTINGS_MODULE 来执行此操作。

DJANGO_SETTINGS_MODULE 的值应该是 Python 路径语法,例如 mysite.settings。请注意,设置模块应位于 Python 导入搜索路径上。

If you are using a virtualenv(which is the best practice), you can paste the relevant exportcommand in the file <path-to-virtualenv>/bin/activate

如果您使用的是virtualenv(这是最佳实践),您可以将相关export命令粘贴到文件中<path-to-virtualenv>/bin/activate

回答by mehdi

It happens to me too, I just now found unintentional.

我也遇到过,我现在才发现是无意的。

I changed "configuration" in top of PyCharm IDE so pycharm confused when try to run django code. Exactly the problem popup when I try to run server with shortcut but when I used terminal to run server, I found django code not having any problem and pycharm can't found setting.

我在 PyCharm IDE 顶部更改了“配置”,因此在尝试运行 django 代码时 pycharm 感到困惑。正是当我尝试使用快捷方式运行服务器时弹出的问题,但是当我使用终端运行服务器时,我发现 django 代码没有任何问题并且 pycharm 找不到设置。

So please check "configuration" in top pycharm's ide maybe you do same mistake :)

因此,请检查顶部 pycharm 的 ide 中的“配置”,也许您会犯同样的错误:)

I'm new in django so be calm if my answer was silly.

我是 Django 新手,所以如果我的回答很愚蠢,请保持冷静。

configuration picture

配置图片

回答by Naqib Hakimi

put this in top of settings.py this will configure django for you

把它放在 settings.py 的顶部,这将为你配置 django

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", __file__)
import django
django.setup()

回答by ppython

Like raratiru answered, you need DJANGO_SETTINGS_MODULEenvironment variable defined with the relative pythonic path to your setting file.

就像 raratiru 回答的那样,您需要DJANGO_SETTINGS_MODULE使用设置文件的相对 pythonic 路径定义环境变量。

OR use your django-admincommandwith the settingsparameter:

或者将您的django-admin命令settings参数一起使用:

django-admin --settings=mysite.settings dbshell