python-2.7django-admindjango-1.9

django-admin runserver error


I set virtualenv for python 2.7

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

next create project

django-admin startproject myproject

create app

cd myproject
django-admin startapp contact

and try run dev server

16% django-admin runserver

I receive an error

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?


Solution

  • If you look at what the manage.py script does (this script was created inside the myproject directory when you ran django-admin startproject myproject), it exports the environment variable DJANGO_SETTINGS_MODULE and then runs django-admin (well, it does it through django.core but it does pretty much that).

    django-admin needs to know the python module of the settings for your project, and that information is present in that environment variable.


    Therefore, inside the myproject directory, you shall either run:

    python manage.py runserver
    

    and the manage.py script will do the environment setup for you, or you can force django-admin to read the settings location from the command line:

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

    The --pythonpath switch is not needed if you included your project directory on the default pyhton path.