在Django中的自定义404/500页面中使用静态文件

时间:2022-11-29 14:12:45

I would like to use some custom CSS and images on my custom 404/500 pages that I made. Django doesn't include the STATIC_URL variable in those pages though. What would be the best way to accomplish this? I also tried making a custom 404/500 view and rendering an arbitrary HTML file but it didn't work out so great.

我想在我制作的自定义404/500页面上使用一些自定义CSS和图像。但是Django在这些页面中不包含STATIC_URL变量。实现这一目标的最佳方法是什么?我也试过制作一个自定义的404/500视图并渲染一个任意的HTML文件,但它没有那么好用。

3 个解决方案

#1


15  

Here's how I would do it:

这是我将如何做到这一点:

# urls or settings
handler500 = 'mysite.views.server_error'

# views
from django.shortcuts import render

def server_error(request):
    # one of the things ‘render’ does is add ‘STATIC_URL’ to
    # the context, making it available from within the template.
    response = render(request, '500.html')
    response.status_code = 500
    return response

It's worth mentioning the reason Django doesn't do this by default:

值得一提的是Django默认不这样做的原因:

“The default 500 view passes no variables to the 500.html template and is rendered with an empty Context to lessen the chance of additional errors.”

“默认的500视图不会将变量传递给500.html模板,并使用空的Context进行渲染,以减少出现其他错误的可能性。”

-- Adrian Holovaty, Django documentation

- Adrian Holovaty,Django文档

#2


2  

I run into the same problem and found a solution which doesn't need custom templates or handlers. From Django 1.4 on you can use the tags get_media_prefix and get_static_prefix to access MEDIA_URL and STATIC_URL when they are not in the context.

我遇到了同样的问题,发现了一个不需要自定义模板或处理程序的解决方案。从Django 1.4开始,您可以使用标签get_media_prefix和get_static_prefix在不在上下文中时访问MEDIA_URL和STATIC_URL。

In my particular case (Django 1.5), I wanted to access some static images in my page 500.html. I just added at the beginning of the template

在我的特定情况下(Django 1.5),我想在我的页面500.html中访问一些静态图像。我刚刚在模板的开头添加了

{% load static %} 

and then obtained the media and static urls with these tags

然后使用这些标签获取媒体和静态网址

<img src="{% get_media_prefix %}logo.png">
<img src="{% get_static_prefix %}img/error_pages/error.png" style="height:235px;">

You can find the official documentation here: https://docs.djangoproject.com/en/1.5/ref/templates/builtins/#get-static-prefix

你可以在这里找到官方文档:https://docs.djangoproject.com/en/1.5/ref/templates/builtins/#get-static-prefix

#3


-1  

I believe you're just going to have to override the default 404/500 error handling. This should get you started:

我相信你只需要覆盖默认的404/500错误处理。这应该让你开始:

http://docs.djangoproject.com/en/dev/topics/http/views/#customizing-error-views

http://docs.djangoproject.com/en/dev/topics/http/views/#customizing-error-views

#1


15  

Here's how I would do it:

这是我将如何做到这一点:

# urls or settings
handler500 = 'mysite.views.server_error'

# views
from django.shortcuts import render

def server_error(request):
    # one of the things ‘render’ does is add ‘STATIC_URL’ to
    # the context, making it available from within the template.
    response = render(request, '500.html')
    response.status_code = 500
    return response

It's worth mentioning the reason Django doesn't do this by default:

值得一提的是Django默认不这样做的原因:

“The default 500 view passes no variables to the 500.html template and is rendered with an empty Context to lessen the chance of additional errors.”

“默认的500视图不会将变量传递给500.html模板,并使用空的Context进行渲染,以减少出现其他错误的可能性。”

-- Adrian Holovaty, Django documentation

- Adrian Holovaty,Django文档

#2


2  

I run into the same problem and found a solution which doesn't need custom templates or handlers. From Django 1.4 on you can use the tags get_media_prefix and get_static_prefix to access MEDIA_URL and STATIC_URL when they are not in the context.

我遇到了同样的问题,发现了一个不需要自定义模板或处理程序的解决方案。从Django 1.4开始,您可以使用标签get_media_prefix和get_static_prefix在不在上下文中时访问MEDIA_URL和STATIC_URL。

In my particular case (Django 1.5), I wanted to access some static images in my page 500.html. I just added at the beginning of the template

在我的特定情况下(Django 1.5),我想在我的页面500.html中访问一些静态图像。我刚刚在模板的开头添加了

{% load static %} 

and then obtained the media and static urls with these tags

然后使用这些标签获取媒体和静态网址

<img src="{% get_media_prefix %}logo.png">
<img src="{% get_static_prefix %}img/error_pages/error.png" style="height:235px;">

You can find the official documentation here: https://docs.djangoproject.com/en/1.5/ref/templates/builtins/#get-static-prefix

你可以在这里找到官方文档:https://docs.djangoproject.com/en/1.5/ref/templates/builtins/#get-static-prefix

#3


-1  

I believe you're just going to have to override the default 404/500 error handling. This should get you started:

我相信你只需要覆盖默认的404/500错误处理。这应该让你开始:

http://docs.djangoproject.com/en/dev/topics/http/views/#customizing-error-views

http://docs.djangoproject.com/en/dev/topics/http/views/#customizing-error-views