setup()

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 to FORCE_SCRIPT_NAME if defined, or / otherwise.
  • Initializing the application registry.
Changed in Django 1.10:

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.

  1. 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.

  2. 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 or models/__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.

  3. Finally Django runs the ready() method of each application configuration.
doc_Django
2016-10-09 18:39:35
Comments
Leave a Comment

Please login to continue.