如何解决DJANGO_SETTINGS_MODULE的问题?

时间:2019-10-01 21:23:18

标签: python django

我是django和编码方面的初学者。卡住了一段时间,并显示一个错误。如果有人帮助我将不胜感激!我尝试用Google搜索它,这给了我很多解决方案,我尝试了一下,但是它也给出了错误。我应该写些什么来使事情完成?我从https://www.youtube.com/watch?v=a48xeeo5Vnk&list=PL-osiE80TeTtoQCKZ03TU5fNfx2UY6U4p&index=2

获得了这段代码
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include('blog.urls')),
]

它给了我这个错误:

  File "urls.py", line 21, in <module>
    path('admin/', admin.site.urls),
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/utils/functional.py", line 256, in inner
    self._setup()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/contrib/admin/sites.py", line 529, in _setup
    AdminSiteClass = import_string(apps.get_app_config('admin').default_site)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/apps/registry.py", line 153, in get_app_config
    self.check_apps_ready()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/apps/registry.py", line 134, in check_apps_ready
    settings.INSTALLED_APPS
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/conf/__init__.py", line 79, in __getattr__
    self._setup(name)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/conf/__init__.py", line 64, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.```

1 个答案:

答案 0 :(得分:0)

确保已将应用程序添加到settings.py中的INSTALLED_APPS中,并且已正确将模型导入admin.py文件中。

如果这不起作用,则可能需要设置DJANGO_SETTINGS_MODULE-

export DJANGO_SETTINGS_MODULE=mysite.settings

您可以在documentation中看到它。