setup(set_prefix=True)
[source]
Configures Django by:
- Loading the settings.
- Setting up logging.
- If
set_prefix
is True, setting the URL resolver script prefix toFORCE_SCRIPT_NAME
if defined, or/
otherwise. - Initializing the application registry.
The ability to set the URL resolver script prefix is new.
This function is called automatically:
- When running an HTTP server via Django’s WSGI support.
- When invoking a management command.
It must be called explicitly in other cases, for instance in plain Python scripts.
The application registry is initialized in three stages. At each stage, Django processes all applications in the order of INSTALLED_APPS
.
-
First Django imports each item in
INSTALLED_APPS
.If it’s an application configuration class, Django imports the root package of the application, defined by its
name
attribute. If it’s a Python package, Django creates a default application configuration.At this stage, your code shouldn’t import any models!
In other words, your applications’ root packages and the modules that define your application configuration classes shouldn’t import any models, even indirectly.
Strictly speaking, Django allows importing models once their application configuration is loaded. However, in order to avoid needless constraints on the order of
INSTALLED_APPS
, it’s strongly recommended not import any models at this stage.Once this stage completes, APIs that operate on application configurations such as
get_app_config()
become usable. -
Then Django attempts to import the
models
submodule of each application, if there is one.You must define or import all models in your application’s
models.py
ormodels/__init__.py
. Otherwise, the application registry may not be fully populated at this point, which could cause the ORM to malfunction.Once this stage completes, APIs that operate on models such as
get_model()
become usable. - Finally Django runs the
ready()
method of each application configuration.
Please login to continue.