Skip to main content
 首页 » 编程设计

python之django 部署-配置不当 : The SECRET_KEY setting must not be empty

2024年10月17日35myhome

该项目在 windows pycharm 上运行良好。

现在我把它放在 ubuntu14.04 和 apache2 上。

当我运行 python manage.py runserver 8000

Traceback (most recent call last):   File "manage.py", line 14, in <module> 
    execute_from_command_line(sys.argv)   File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/core/management/__init__.py", line 350, in execute_from_command_line 
    utility.execute()   File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/core/management/__init__.py", line 342, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv)   File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/core/management/__init__.py", line 195, in fetch_command 
    klass = load_command_class(app_name, subcommand)   File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/core/management/__init__.py", line 39, in load_command_class 
    module = import_module('%s.management.commands.%s' % (app_name, name))   File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module 
    __import__(name)   File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/core/management/commands/runserver.py", line 16, in <module> 
    from django.db.migrations.executor import MigrationExecutor   File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/db/migrations/executor.py", line 7, in <module> 
    from .loader import MigrationLoader   File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/db/migrations/loader.py", line 10, in <module> 
    from django.db.migrations.recorder import MigrationRecorder   File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/db/migrations/recorder.py", line 12, in <module> 
    class MigrationRecorder(object):   File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/db/migrations/recorder.py", line 26, in MigrationRecorder 
    class Migration(models.Model):   File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/db/migrations/recorder.py", line 27, in Migration 
    app = models.CharField(max_length=255)   File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/db/models/fields/__init__.py", line 1072, in __init__ 
    super(CharField, self).__init__(*args, **kwargs)   File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/db/models/fields/__init__.py", line 166, in __init__ 
    self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE   File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/conf/__init__.py", line 55, in __getattr__ 
    self._setup(name)   File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/conf/__init__.py", line 43, in _setup 
    self._wrapped = Settings(settings_module)   File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/conf/__init__.py", line 99, in __init__ 
    mod = importlib.import_module(self.SETTINGS_MODULE)   File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module 
    __import__(name)   File "/var/www/Web-Interaction-APP/settings.py", line 8, in <module> 
    application = get_wsgi_application()   File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/core/wsgi.py", line 13, in get_wsgi_application 
    django.setup()   File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/__init__.py", line 17, in setup 
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)   File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/conf/__init__.py", line 55, in __getattr__ 
    self._setup(name)   File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/conf/__init__.py", line 43, in _setup 
    self._wrapped = Settings(settings_module)   File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/conf/__init__.py", line 120, in __init__ 
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.") django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty. 

我还尝试更改我的 manage.py 和 wsgi.py。我的文件夹是:

Web-Interaction-APP 
 
-manage.py   
 
-seetings.py 
 
*apache(folder) 
 
       -wsgi.py 
 
 *project(folder) 

在我的 manage.py 中:

#!/usr/bin/env python 
#This file runs the server for this application from command line. 
import os, sys 
 
#Executes the code in the following condition if the program is run directly.  
#(Source: http://stackoverflow.com/questions/419163/what-does-if-name-main-do) 
if __name__ == "__main__": 
    #Refers to the path of this django project's settings module. 
    #(DJANGO_SETTINGS_MODULE is an environment variable to module import path.) 
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") 
 
    from django.core.management import execute_from_command_line 
    #Executes the django project. 
    execute_from_command_line(sys.argv) 

在我的 wsgi.py 中:

import os, sys 
# Calculate the path based on the location of the WSGI script. 
apache_configuration= os.path.dirname(__file__) 
project = os.path.dirname(apache_configuration) 
workspace = os.path.dirname(project) 
sys.path.append(workspace) 
sys.path.append(project) 
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Web-Interaction-APP.settings") 
 
 
# Add the path to 3rd party django application and to django itself. 
sys.path.append('/home/zhaojf1') 
os.environ['DJANGO_SETTINGS_MODULE'] = '10.231.52.XX.apache.override' 
import django.core.handlers.wsgi 
application = django.core.handlers.wsgi.WSGIHandler() 

请您参考如下方法:

也许在您的设置文件中,您正在使用 ENV 变量设置 SECRET_KEY,打开一个 shell,然后:

 export SECRET_KEY='test_key' 

然后,再次尝试运行您的服务器。