Django:404模板中的静态文件 [英] Django: static files in a 404 template

查看:159
本文介绍了Django:404模板中的静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用默认视图将样式表和图像包含到404页面模板中?

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

我创建了一个 404.html 文件在网站的模板目录的根目录中:

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>

讽刺的是, 404.css 不是找到。 404.css 文件位于其中一个应用程序的 static 目录中。

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

服务器是 manage.py runserver

更新:设置 DEBUG = False settings.py 中,所有其他页面上的静态文件也停止提供。

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

推荐答案

出现 staticfiles 应用程序与 DEBUG = False 一起工作。只有它不从单个应用程序的静态目录中拾取文件。它将从全局 STATIC_ROOT 目录(从 settings.py )中提供文件。

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

要将静态文件复制到 STATIC_ROOT ,您需要运行 collectstatic 命令

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

python manage.py collectstatic

这篇关于Django:404模板中的静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆