2017-09-17 43 views
1

我有一個Django項目。我使用的軟件包:Django 1.11,Eventlet 0.21.0。 Python版本是3.4.3。 有以下配置文件:eventlet.monkey_patch()拋出異常:SECRET_KEY設置不能爲空

1)主項目的配置文件settings.py。導入其他兩個配置文件中的一個是主要角色。

try: 
    from .settings_local import * 
except ImportError: 
    from .conf.common import * 

2)settings_local.py。此文件的目的是從common.py導入所有設置,並覆蓋這些文件的體內(或者增加一些新的設置),例如用於測試目的:

from .conf.common import * 
... 

3)common.py 。包含所有配置設置的文件。

import eventlet 
eventlet.monkey_patch(all=True, MySQLdb=True) 
... 

當我試圖檢查的配置,我得到以下錯誤:

./manage.py check 

Traceback (most recent call last): 
    File "./manage.py", line 10, in <module> 
    execute_from_command_line(sys.argv) 
    File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line 
    utility.execute() 
    File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/core/management/__init__.py", line 355, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/core/management/base.py", line 283, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/core/management/base.py", line 322, in execute 
    saved_locale = translation.get_language() 
    File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 195, in get_language 
    return _trans.get_language() 
    File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 59, in __getattr__ 
    if settings.USE_I18N: 
    File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/conf/__init__.py", line 56, in __getattr__ 
    self._setup(name) 
    File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/conf/__init__.py", line 41, in _setup 
    self._wrapped = Settings(settings_module) 
    File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/conf/__init__.py", line 110, in __init__ 
    mod = importlib.import_module(self.SETTINGS_MODULE) 
    File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/importlib/__init__.py", line 109, in import_module 
    return _bootstrap._gcd_import(name[level:], package, level) 
    File "<frozen importlib._bootstrap>", line 2254, in _gcd_import 
    File "<frozen importlib._bootstrap>", line 2237, in _find_and_load 
    File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked 
    File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked 
    File "<frozen importlib._bootstrap>", line 1129, in _exec 
    File "<frozen importlib._bootstrap>", line 1471, in exec_module 
    File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed 
    File "/vagrant/<project_name>/<project_name>/<project_name>/settings.py", line 2, in <module> 
    from .settings_local import * 
    File "/vagrant/<project_name>/<project_name>/<project_name>/settings_local.py", line 1, in <module> 
    from .conf.common import * 
    File "/vagrant/<project_name>/<project_name>/<project_name>/conf/common.py", line 3, in <module> 
    eventlet.monkey_patch(all=True, MySQLdb=True) 
    File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/eventlet/patcher.py", line 254, in monkey_patch 
    _green_existing_locks() 
    File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/eventlet/patcher.py", line 345, in _green_existing_locks 
    if isinstance(obj, rlock_type): 
    File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/utils/functional.py", line 238, in inner 
    self._setup() 
    File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/conf/__init__.py", line 41, in _setup 
    self._wrapped = Settings(settings_module) 
    File "/home/vagrant/.virtualenvs/<project_name>/lib/python3.4/site-packages/django/conf/__init__.py", line 129, in __init__ 
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.") 
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty. 

我想通了,異常最初發生在/lib/python3.4/site-packages/eventlet/patcher .py函數_green_existing_locks()然後傳播到__init.py__

有人能告訴我我做錯了什麼嗎?

回答

0

django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

跟隨誤差意味着SECRET_KEY未設置

+0

它定義在** common.py ** –

+0

正如@temoto回答的設置應該始終在調用Django啓動之前 – iklinac

2

請遵守設置和代碼之間的區別設置。

# settings.py 
SECRET_KEY = 'abc...' 
DEBUG = True 
# just a list of constant definitions 

# your common.py 
eventlet.monkey_patch() 
# it's code that changes python environment 

# another bad example of settings.py with similar problem 
open('/tmp/flag-file-1', 'w') 
os.remove('/tmp/flag-file-2') 

您不會將副作用生成代碼放入設置中。根據您的版本的Django準則查找初始化代碼的位置,並在那裏放置monkey_patch()