django-admin runserver错误

时间:2016-05-20 19:13:08

标签: python-2.7 django-admin django-1.9

我为python 2.7设置了virtualenv

16% source Work/Django/env/bin/activate

下一个创建项目

django-admin startproject myproject

创建应用

cd myproject
django-admin startapp contact

并尝试运行dev server

16% django-admin runserver

我收到错误

  

追踪(最近一次通话):     文件" / home / dima / Work / Django / env / bin / django-admin",第11行,in       sys.exit(execute_from_command_line())     文件" /home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/core/management/ init .py",第353行,在execute_from_command_line中       utility.execute()     文件" /home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/core/management/ init .py",第345行,执行中       self.fetch_command(子命令).run_from_argv(self.argv)     文件" /home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/core/management/ init .py",第195行,在fetch_command中       klass = load_command_class(app_name,子命令)     文件" /home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/core/management/ init .py",第39行,在load_command_class中       module = import_module('%s.management.commands。%s'%(app_name,name))     在import_module中输入文件" /usr/lib/python2.7/importlib/ init .py",第37行       导入(名称)     文件" /home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py" ;,第16行,在       从django.db.migrations.executor导入MigrationExecutor     文件" /home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/db/migrations/executor.py" ;,第7行,in       来自.loader导入MigrationLoader     文件" /home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/db/migrations/loader.py" ;,第10行,在       来自django.db.migrations.recorder导入MigrationRecorder     文件" /home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/db/migrations/recorder.py" ;,第12行,in       class MigrationRecorder(object):     在MigrationRecorder中的文件" /home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/db/migrations/recorder.py" ;,第26行       class Migration(models.Model):     文件" /home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/db/migrations/recorder.py" ;,第27行,在迁移中       app = models.CharField(max_length = 255)     文件" /home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/db/models/fields/ init .py",第1072行, init       super(CharField,self)。 init (* args,** kwargs)     文件" /home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/db/models/fields/ init .py",第166行,在 init 中       self.db_tablespace = db_tablespace或settings.DEFAULT_INDEX_TABLESPACE     文件" /home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/conf/ init .py",第55行,in的 GETATTR       self._setup(名称)     文件" /home/dima/Work/Django/env/local/lib/python2.7/site-packages/django/conf/ init .py",第41行,在_建立       %(desc,ENVIRONMENT_VARIABLE))   django.core.exceptions.ImproperlyConfigured:请求设置DEFAULT_INDEX_TABLESPACE,但未配置设置。您必须在访问设置之前定义环境变量DJANGO_SETTINGS_MODULE或调用settings.configure()。   如何修复此错误?

2 个答案:

答案 0 :(得分:3)

如果查看aaa => Hello bbb => World ccc => Test! 脚本的作用(此脚本是在manage.py运行myproject时在django-admin startproject myproject目录中创建的),则会导出环境变量DJANGO_SETTINGS_MODULE和然后运行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开关。

答案 1 :(得分:0)

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)