django deploy- ImproperlyConfigured:SECRET_KEY设置不能为空

时间:2016-08-03 19:21:45

标签: python django ubuntu deployment

该项目在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.pywsgi.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()

2 个答案:

答案 0 :(得分:0)

当您DJANGO_SETTINGS_MODULE path is incorrect

时,此错误很常见

当您运行python manage.py runserver 8000时,您正在使用manage.py设置DJANGO_SETTINGS_MODULEsettings。您的根目录中似乎没有settings.py,因此manage.py中的这一行:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

应该成为:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Web-Interaction-APP.settings")

就像在wsgi.py文件中一样。

答案 1 :(得分:0)

也许在您的设置文件中,您使用SECRET_KEY变量设置ENV,打开shell并且只是:

 export SECRET_KEY='test_key'

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