Commit 7d81ee6e authored by Claude Paroz's avatar Claude Paroz
Browse files

Fixed #16734 -- Set script prefix even outside of requests

Thanks Tim Graham for the review.
parent 9dcfecb7
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
from __future__ import unicode_literals

from django.utils.version import get_version

VERSION = (1, 10, 0, 'alpha', 0)
@@ -5,14 +7,21 @@ VERSION = (1, 10, 0, 'alpha', 0)
__version__ = get_version(VERSION)


def setup():
def setup(set_prefix=True):
    """
    Configure the settings (this happens as a side effect of accessing the
    first setting), configure logging and populate the app registry.
    Set the thread-local urlresolvers script prefix if `set_prefix` is True.
    """
    from django.apps import apps
    from django.conf import settings
    from django.core.urlresolvers import set_script_prefix
    from django.utils.encoding import force_text
    from django.utils.log import configure_logging

    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
    if set_prefix:
        set_script_prefix(
            '/' if settings.FORCE_SCRIPT_NAME is None else force_text(settings.FORCE_SCRIPT_NAME)
        )
    apps.populate(settings.INSTALLED_APPS)
+1 −1
Original line number Diff line number Diff line
@@ -10,5 +10,5 @@ def get_wsgi_application():
    Allows us to avoid making django.core.handlers.WSGIHandler public API, in
    case the internal WSGI implementation changes or moves in the future.
    """
    django.setup()
    django.setup(set_prefix=False)
    return WSGIHandler()
+7 −1
Original line number Diff line number Diff line
@@ -332,14 +332,20 @@ application registry.

.. currentmodule:: django

.. function:: setup()
.. function:: setup(set_script=True)

    Configures Django by:

    * Loading the settings.
    * Setting up logging.
    * If ``set_script`` is True, setting the URL resolver script prefix to
      :setting:`FORCE_SCRIPT_NAME` if defined, or ``/`` otherwise.
    * Initializing the application registry.

    .. versionchanged:: 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.
+8 −1
Original line number Diff line number Diff line
@@ -1408,7 +1408,14 @@ Default: ``None``
If not ``None``, this will be used as the value of the ``SCRIPT_NAME``
environment variable in any HTTP request. This setting can be used to override
the server-provided value of ``SCRIPT_NAME``, which may be a rewritten version
of the preferred value or not supplied at all.
of the preferred value or not supplied at all. It is also used by
:func:`django.setup()` to set the URL resolver script prefix outside of the
request/response cycle (e.g. in management commands and standalone scripts) to
generate correct URLs when ``SCRIPT_NAME`` is not ``/``.

.. versionchanged:: 1.10

    The setting's use in :func:`django.setup()` was added.

.. setting:: FORMAT_MODULE_PATH

+4 −1
Original line number Diff line number Diff line
@@ -209,7 +209,10 @@ Tests
URLs
^^^^

* ...
* An addition in :func:`django.setup()` allows URL resolving that happens
  outside of the request/response cycle (e.g. in management commands and
  standalone scripts) to take :setting:`FORCE_SCRIPT_NAME` into account when it
  is set.

Validators
^^^^^^^^^^
Loading