0

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

我在404.html站点目录的根目录中创建了一个文件templates

<!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目录之一中。

服务器是manage.py runserver. 在每个其他页面上,静态文件都提供得很好。

更新:似乎在设置后DEBUG = Falsesettings.py所有其他页面上的静态文件也停止提供服务。

4

3 回答 3

2

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
于 2011-10-30T17:00:09.273 回答
0

当我使用 DEBUG=True 时,我通常在我的根 urlconf 中有这个片段:

(r'^static/(?P<path>.*)$', 'django.views.static.serve',
    {'document_root': settings.STATIC_ROOT}),
于 2011-10-31T00:54:35.490 回答
0

由于竞赛处理器.static 已经在 TEMPLATE_CONTEXT 中!您可以在模板中使用变量 STATIC_URL:follow doc:

https://docs.djangoproject.com/en/1.3/ref/templates/api/#django-core-context-processors-static

如果 TEMPLATE_CONTEXT_PROCESSORS 包含此处理器,则每个 RequestContext 都将包含一个变量 STATIC_URL,提供 STATIC_URL 设置的值。

而且我们还知道 404 视图处理程序使用 RequestContext 对象,遵循从 django.views.defaults.py 获取的代码:

return http.HttpResponseNotFound(t.render(RequestContext(request, {'request_path': request.path}))) 

所以只需{{ STATIC_URL }}在您的模板中使用,它应该可以工作!

于 2011-10-30T17:02:11.390 回答