Phoenix Phoenix - 1 month ago 19
Python Question

Python documentation for django.views.generic

When I run in command line (terminal in ubuntu) following command:

pydoc django.views.generic


I have got following error:

problem in django.views.generic -
<class '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 it can be resolved in order to access for help document?

Answer

Importing many of Django's modules pulls in other modules that depend on being in a Django project environment. Create a dummy project, and use its settings.

$ django-admin.py startproject dummy
$ cd ./dummy
$ DJANGO_SETTINGS_MODULE=dummy.settings pydoc django.views.generic

Note the last line that sets the env var DJANGO_SETTINGS_MODULE to the import path of the settings.py file. Normally, you don't need this since manage.py and wsgi.py set this up for you, but pydoc knows nothing about this.

If you already have a project, you can obviously skip the dummy project and just use your project's settings.


When using newer versions of Django, you also need to call django.setup() before Django is generally usable. Wrap pydoc in a script to do this, rather than calling pydoc directly.

my_pydoc.py

import django
import pydoc
import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'dummy.settings'
django.setup()
pydoc.cli()
python my_pydoc.py django.views.generic
Comments