2014-09-28 267 views
29

我試圖將我的django項目配置爲部署到heroku。我收到以下錯誤,我不知道如何修復它。錯誤配置:您必須先定義環境變量DJANGO_SETTINGS_MODULE或在訪問設置之前調用settings.configure()

這裏是全追蹤和錯誤:

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()) 

而如果是相關的,我manage.py file:

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) 

任何人都可以在這裏理解這個問題嗎?如果是這樣,你能善意地解釋什麼是錯的?謝謝!

回答

48

我盤算了一下,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並將代碼行底部添加​​來自動執行該過程。從現在開始,每次激活虛擬環境時,您都將使用該應用程序的設置。

29

The Definitive Guide to Django: Web Development Done Right

If you’ve used Python before, you may be wondering why we’re running python manage.py shell instead of just python. Both commands will start the interactive interpreter, but the manage.py shell command has one key difference: before starting the interpreter, it tells Django which settings file to use.


"Use Case" :
Many parts of Django**, including the template system, rely on your settings, and you won’t be able to use them unless the framework knows which settings to use.

If you’re curious, here’s how it works behind the scenes. Django looks for an environment variable called DJANGO_SETTINGS_MODULE, which should be set to the import path of your settings.py. **For example, DJANGO_SETTINGS_MODULE might be set to 'mysite.settings', assuming mysite is on your Python path.

"Simply": When you run python manage.py shell, the command takes care of setting DJANGO_SETTINGS_MODULE for you.
We’re encouraging you to use python manage.py shell in these examples so as to minimize the amount of tweaking and configuring you have to do.

+0

謝謝你。雖然django shell拋出了同樣的錯誤,但我最終解決了這個問題。 我在一個特定的情況下,其中我沒有'settings.py'文件/模塊在我的代碼庫,但'settings'包/文件夾。該軟件包包含一個'__init __。py'文件,其中包含'settings.py'的所有常規內容,以及其他本地設置文件,這些文件在__init__中導入。 所以我必須配置如下: 'export DJANGO_SETTINGS_MODULE = myappfolder.settings .__ init__' – 2017-09-15 15:47:46

-1

創建.ENV文件,將在項目的根持有憑據,然後離開它,版本控制:

$ echo ".env" >> .gitignore 

在.ENV文件,添加變量(根據您的安裝調整它們):

$ echo "DJANGO_SETTINGS_MODULE=myproject.settings.production"> .env 
#50 caracter random key 
$ echo "SECRET_KEY='####'">> .env 

要使用它們,把這個在您的production.py設置文件的頂部:

import os 

env = os.environ.copy() 
SECRET_KEY = env['SECRET_KEY'] 

使用此寶石將其發佈到Heroku的:http://github.com/ddollar/heroku-config.git

$ heroku plugins:install git://github.com/ddollar/heroku-config.git 
$ heroku config:push 

這樣你可以避免改變的virtualenv文件。

*在此基礎上tutorial

9

的Django需要你的應用程序特定的設置。因爲它已經在您的manage.py中。更快,但也許是暫時的解決方案是::

python manage.py shell 
+0

這正是我在閱讀[Django 2.0教程 - 第5部分 - 自動測試]時所需要的(https:// docs.djangoproject.com/en/2.0/intro/tutorial05/)。以前,我通過單獨輸入'python'在我的環境中進入了Python交互模式。這會在輸入'from polls.models import question'後導致AppRegistryNotReady異常。 – myidealab 2018-01-31 20:38:01