gpt4 book ai didi

python - ImproperlyConfigured : You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings. configure() 在访问设置之前

转载 作者:IT老高 更新时间:2023-10-28 20:30:25 24 4
gpt4 key购买 nike

我试图配置我的 Django 项目以部署到 Heroku。我收到以下错误,我真的不知道如何解决它。

这是完整的追溯和错误:

22:46:15 web.1  | Traceback (most recent call last):
22:46:15 web.1 | File "/Users/nir/nirla/venv/lib/python2.7/site-packages/gunicorn/arbiter.py", line 495, in spawn_worker
22:46:15 web.1 | worker.init_process()
22:46:15 web.1 | File "/Users/nir/nirla/venv/lib/python2.7/site-packages/gunicorn/workers/base.py", line 106, in init_process
22:46:15 web.1 | self.wsgi = self.app.wsgi()
22:46:15 web.1 | File "/Users/nir/nirla/venv/lib/python2.7/site-packages/gunicorn/app/base.py", line 114, in wsgi
22:46:15 web.1 | self.callable = self.load()
22:46:15 web.1 | File "/Users/nir/nirla/venv/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 62, in load
22:46:15 web.1 | return self.load_wsgiapp()
22:46:15 web.1 | File "/Users/nir/nirla/venv/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load_wsgiapp
22:46:15 web.1 | return util.import_app(self.app_uri)
22:46:15 web.1 | File "/Users/nir/nirla/venv/lib/python2.7/site-packages/gunicorn/util.py", line 354, in import_app
22:46:15 web.1 | __import__(module)
22:46:15 web.1 | File "/Users/nir/nirla/nirla/wsgi.py", line 12, in <module>
22:46:15 web.1 | from dj_static import Cling
22:46:15 web.1 | File "/Users/nir/nirla/venv/lib/python2.7/site-packages/dj_static.py", line 7, in <module>
22:46:15 web.1 | from django.contrib.staticfiles.handlers import StaticFilesHandler as DebugHandler
22:46:15 web.1 | File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 8, in <module>
22:46:15 web.1 | from django.contrib.staticfiles.views import serve
22:46:15 web.1 | File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/contrib/staticfiles/views.py", line 13, in <module>
22:46:15 web.1 | from django.views import static
22:46:15 web.1 | File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/views/static.py", line 96, in <module>
22:46:15 web.1 | template_translatable = ugettext_noop("Index of %(directory)s")
22:46:15 web.1 | File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 65, in gettext_noop
22:46:15 web.1 | return _trans.gettext_noop(message)
22:46:15 web.1 | File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 52, in __getattr__
22:46:15 web.1 | if settings.USE_I18N:
22:46:15 web.1 | File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
22:46:15 web.1 | self._setup(name)
22:46:15 web.1 | File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/conf/__init__.py", line 47, in _setup
22:46:15 web.1 | % (desc, ENVIRONMENT_VARIABLE))
22:46:15 web.1 | 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.

这是我的 wsgi.py 文件:

import os
from django.core.wsgi import get_wsgi_application
from dj_static import Cling

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "nirla.settings") #nirla is the name of the project


application = Cling(get_wsgi_application())

如果相关,我的 ma​​nage.py 文件:

import os
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "nirla.settings")

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)

似乎有人能理解这里的问题吗?如果是这样,你能解释一下哪里出了问题吗?

最佳答案

我认为必须以某种方式设置 DJANGO_SETTINGS_MODULE,所以我查看了 documentation (链接已更新)并找到:

export DJANGO_SETTINGS_MODULE=mysite.settings

虽然如果您在 heroku 上运行服务器,这还不够,您还需要在那里指定它。像这样:

heroku config:set DJANGO_SETTINGS_MODULE=mysite.settings --account <your account name> 

在我的具体情况下,我运行了这两个,一切都解决了:

export DJANGO_SETTINGS_MODULE=nirla.settings
heroku config:set DJANGO_SETTINGS_MODULE=nirla.settings --account personal

编辑

我还想指出,每次关闭或重新启动虚拟环境时都必须重新执行此操作。相反,您应该通过转到 venv/bin/activate 并在代码底部添加以下行来自动化该过程:set DJANGO_SETTINGS_MODULE=mysite.settings。从现在开始,每次激活虚拟环境时,您都将使用该应用的设置。

关于python - ImproperlyConfigured : You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings. configure() 在访问设置之前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26082128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com