Search code examples
pythondjangodjango-templatesstatic-filesdjango-staticfiles

Django: static files in a 404 template


How to include stylesheets and images into a 404 page template using the default view?

I created a 404.html file in the root of the site's templates directory:

<!DOCTYPE html>
<html>
<head>
    {% load static %}
    <link rel="stylesheet" href="{% get_static_prefix %}css/404.css" />
</head>
<body class="page-404">
    <p>Not found.</p>
</body>
</html>

Ironically, the 404.css is not found. The 404.css file is located in one of the apps' static directory.

The server is manage.py runserver. On every other page static files are served just well.

Update: It appears, after setting DEBUG = False in the settings.py, static files on all other pages stopped being served, too.


Solution

  • It appears, staticfiles app does work with DEBUG = False. Only it doesn’t pick up files from individual apps’ static directories. It would serve files from the global STATIC_ROOT directory (from settings.py).

    To get the static files copied to STATIC_ROOT, you need to run the collectstatic command:

    python manage.py collectstatic