Phoenix Phoenix - 1 year ago 225
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'>:
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 Source

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.

$ 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 file. Normally, you don't need this since and 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.

import django
import pydoc
import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'dummy.settings'
python django.views.generic
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download