django-admin 运行服务器错误

django-admin runserver error

我为 python 2.7

设置了 virtualenv
16% source Work/Django/env/bin/activate

接下来创建项目

django-admin startproject myproject

创建应用程序

cd myproject
django-admin startapp contact

并尝试 运行 开发服务器

16% django-admin runserver

我收到一个错误

Traceback (most recent call last): File "/home/dima/Work/Django/env/bin/django-admin", line 11, in sys.exit(execute_from_command_line()) File "/home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/core/management/init.py", line 353, in execute_from_command_line utility.execute() File "/home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/core/management/init.py", line 345, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/core/management/init.py", line 195, in fetch_command klass = load_command_class(app_name, subcommand) File "/home/dima/Work/Django/env/local/lib/python2.7/site-packages/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 "/home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 16, in from django.db.migrations.executor import MigrationExecutor File "/home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 7, in from .loader import MigrationLoader File "/home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 10, in from django.db.migrations.recorder import MigrationRecorder File "/home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 12, in class MigrationRecorder(object): File "/home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 26, in MigrationRecorder class Migration(models.Model): File "/home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 27, in Migration app = models.CharField(max_length=255) File "/home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/db/models/fields/init.py", line 1072, in init super(CharField, self).init(*args, **kwargs) File "/home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/db/models/fields/init.py", line 166, in init self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE File "/home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/conf/init.py", line 55, in getattr self._setup(name) File "/home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/conf/init.py", line 41, in _setup % (desc, ENVIRONMENT_VARIABLE)) django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. How fix this error?

如果您查看 manage.py 脚本的作用(此脚本是在您 运行 django-admin startproject myproject 时在 myproject 目录中创建的),它会导出环境变量DJANGO_SETTINGS_MODULE 然后是 运行s django-admin(好吧,它通过 django.core 做到了,但它几乎做到了)。

django-admin 需要知道项目设置的 python 模块,并且该信息存在于该环境变量中。


因此,在 myproject 目录中,您应该 运行:

python manage.py runserver

manage.py 脚本将为您设置环境, 您可以强制 django-admin 从命令行读取设置位置:

django-admin runserver --pythonpath=. --settings="myproject.settings"

如果您将项目目录包含在默认的 pyhton 路径中,则不需要 --pythonpath 开关。

mkdir djangoproject

cd djangoproject/

pip install virtualenv

virtualenv myvenv

pip freeze

which python

source myvenv/bin/activate

pip install django

pip freeze

django-admin startproject myproject

cd myproject/

python manage.py runserver

您的项目将成功启动

django-admin startapp myapp

现在,您需要进行一些与您创建的应用程序相关的设置。 (settings.py & urls.py)