<commit_before>=========================================================
How to manage static files (e.g. images, JavaScript, CSS)
=========================================================

Websites generally need to serve additional files such as images, JavaScript,
or CSS. In Django, we refer to these files as "static files".  Django provides
:mod:`django.contrib.staticfiles` to help you manage them.

This page describes how you can serve these static files.

Configuring static files
========================

#. Make sure that ``django.contrib.staticfiles`` is included in your
   :setting:`INSTALLED_APPS`.

#. In your settings file, define :setting:`STATIC_URL`, for example::

      STATIC_URL = 'static/'

#. In your templates, use the :ttag:`static` template tag to build the URL for
   the given relative path using the configured :setting:`STATICFILES_STORAGE`.

   .. _staticfiles-in-templates:

   .. code-block:: html+django

        {% load static %}
        <img src="{% static 'my_app/example.jpg' %}" alt="My image">

#. Store your static files in a folder called ``static`` in your app. For
   example ``my_app/static/my_app/example.jpg``.

.. admonition:: Serving the files

    In addition to these configuration steps, you'll also need to actually
    serve the static files.

    During development, if you use :mod:`django.contrib.staticfiles`, this will
    be done automatically by :djadmin:`runserver` when :setting:`DEBUG` is set
    to ``True`` (see :func:`django.contrib.staticfiles.views.serve`).

    This method is **grossly inefficient** and probably **insecure**,
    so it is **unsuitable for production**.

    See :doc:`/howto/static-files/deployment` for proper strategies to serve
    static files in production environments.

Your project will probably also have static assets that aren't tied to a
particular app. In addition to using a ``static/`` directory inside your apps,
you can define a list of directories (:setting:`STATICFILES_DIRS`) in your
settings file where Django will also look for static files. For example::

    STATICFILES_DIRS = [
        BASE_DIR / "static",
        '/var/www/static/',
    ]

See the documentation for the :setting:`STATICFILES_FINDERS` setting for
details on how ``staticfiles`` finds your files.

.. admonition:: Static file namespacing

    Now we *might* be able to get away with putting our static files directly
    in ``my_app/static/`` (rather than creating another ``my_app``
    subdirectory), but it would actually be a bad idea. Django will use the
    first static file it finds whose name matches, and if you had a static file
    with the same name in a *different* application, Django would be unable to
    distinguish between them. We need to be able to point Django at the right
    one, and the best way to ensure this is by *namespacing* them. That is,
    by putting those static files inside *another* directory named for the
    application itself.

    You can namespace static assets in :setting:`STATICFILES_DIRS` by
    specifying :ref:`prefixes <staticfiles-dirs-prefixes>`.

.. _serving-static-files-in-development:

Serving static files during development
=======================================

If you use :mod:`django.contrib.staticfiles` as explained above,
:djadmin:`runserver` will do this automatically when :setting:`DEBUG` is set
to ``True``. If you don't have ``django.contrib.staticfiles`` in
:setting:`INSTALLED_APPS`, you can still manually serve static files using the
:func:`django.views.static.serve` view.

This is not suitable for production use! For some common deployment
strategies, see :doc:`/howto/static-files/deployment`.

For example, if your :setting:`STATIC_URL` is defined as ``static/``, you can
do this by adding the following snippet to your urls.py::

    from django.conf import settings
    from django.conf.urls.static import static

    urlpatterns = [
        # ... the rest of your URLconf goes here ...
    ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

.. note::

    This helper function works only in debug mode and only if
    the given prefix is local (e.g. ``static/``) and not a URL (e.g.
    ``http://static.example.com/``).

    Also this helper function only serves the actual :setting:`STATIC_ROOT`
    folder; it doesn't perform static files discovery like
    :mod:`django.contrib.staticfiles`.

.. _serving-uploaded-files-in-development:

Serving files uploaded by a user during development
===================================================

During development, you can serve user-uploaded media files from
:setting:`MEDIA_ROOT` using the :func:`django.views.static.serve` view.

This is not suitable for production use! For some common deployment
strategies, see :doc:`/howto/static-files/deployment`.

For example, if your :setting:`MEDIA_URL` is defined as ``media/``, you can do
this by adding the following snippet to your :setting:`ROOT_URLCONF`::

    from django.conf import settings<commit_msg>Fixed #33048 -- Doc'd that DEBUG static files requests don't use middleware chain.<commit_after>=========================================================
How to manage static files (e.g. images, JavaScript, CSS)
=========================================================

Websites generally need to serve additional files such as images, JavaScript,
or CSS. In Django, we refer to these files as "static files".  Django provides
:mod:`django.contrib.staticfiles` to help you manage them.

This page describes how you can serve these static files.

Configuring static files
========================

#. Make sure that ``django.contrib.staticfiles`` is included in your
   :setting:`INSTALLED_APPS`.

#. In your settings file, define :setting:`STATIC_URL`, for example::

      STATIC_URL = 'static/'

#. In your templates, use the :ttag:`static` template tag to build the URL for
   the given relative path using the configured :setting:`STATICFILES_STORAGE`.

   .. _staticfiles-in-templates:

   .. code-block:: html+django

        {% load static %}
        <img src="{% static 'my_app/example.jpg' %}" alt="My image">

#. Store your static files in a folder called ``static`` in your app. For
   example ``my_app/static/my_app/example.jpg``.

.. admonition:: Serving the files

    In addition to these configuration steps, you'll also need to actually
    serve the static files.

    During development, if you use :mod:`django.contrib.staticfiles`, this will
    be done automatically by :djadmin:`runserver` when :setting:`DEBUG` is set
    to ``True`` (see :func:`django.contrib.staticfiles.views.serve`).

    This method is **grossly inefficient** and probably **insecure**,
    so it is **unsuitable for production**.

    See :doc:`/howto/static-files/deployment` for proper strategies to serve
    static files in production environments.

Your project will probably also have static assets that aren't tied to a
particular app. In addition to using a ``static/`` directory inside your apps,
you can define a list of directories (:setting:`STATICFILES_DIRS`) in your
settings file where Django will also look for static files. For example::

    STATICFILES_DIRS = [
        BASE_DIR / "static",
        '/var/www/static/',
    ]

See the documentation for the :setting:`STATICFILES_FINDERS` setting for
details on how ``staticfiles`` finds your files.

.. admonition:: Static file namespacing

    Now we *might* be able to get away with putting our static files directly
    in ``my_app/static/`` (rather than creating another ``my_app``
    subdirectory), but it would actually be a bad idea. Django will use the
    first static file it finds whose name matches, and if you had a static file
    with the same name in a *different* application, Django would be unable to
    distinguish between them. We need to be able to point Django at the right
    one, and the best way to ensure this is by *namespacing* them. That is,
    by putting those static files inside *another* directory named for the
    application itself.

    You can namespace static assets in :setting:`STATICFILES_DIRS` by
    specifying :ref:`prefixes <staticfiles-dirs-prefixes>`.

.. _serving-static-files-in-development:

Serving static files during development
=======================================

If you use :mod:`django.contrib.staticfiles` as explained above,
:djadmin:`runserver` will do this automatically when :setting:`DEBUG` is set
to ``True``. If you don't have ``django.contrib.staticfiles`` in
:setting:`INSTALLED_APPS`, you can still manually serve static files using the
:func:`django.views.static.serve` view.

This is not suitable for production use! For some common deployment
strategies, see :doc:`/howto/static-files/deployment`.

For example, if your :setting:`STATIC_URL` is defined as ``static/``, you can
do this by adding the following snippet to your urls.py::

    from django.conf import settings
    from django.conf.urls.static import static

    urlpatterns = [
        # ... the rest of your URLconf goes here ...
    ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

.. note::

    This helper function works only in debug mode and only if
    the given prefix is local (e.g. ``static/``) and not a URL (e.g.
    ``http://static.example.com/``).

    Also this helper function only serves the actual :setting:`STATIC_ROOT`
    folder; it doesn't perform static files discovery like
    :mod:`django.contrib.staticfiles`.

    Finally, static files are served via a wrapper at the WSGI application
    layer. As a consequence, static files requests do not pass through the
    normal :doc:`middleware chain </topics/http/middleware>`.

.. _serving-uploaded-files-in-development:

Serving files uploaded by a user during development
===================================================

During development, you can serve user-uploaded media files from
:setting:`MEDIA_ROOT` using the :func:`django.views.static.serve` view.

This is not suitable for production use! For some common deployment
strategies, see :doc:`/howto/static-files/deployment`.

For example, if your :setting:`MEDIA_URL` is defined as ``media/``, you can do
this by adding the following snippet to your :setting:`ROOT_URLCONF`::

    from django.conf import settings
{'fraction_non_alphanumeric': 0.1320295777388597, 'fraction_numerical': 0.0004864759680871765, 'mean_word_length': 4.1497995991983965, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4802710', 'n_tokens_mistral': 2810, 'n_tokens_neox': 2638, 'n_words': 1178}