Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django在自定义404页面上传输带有MIME类型text/html的样式表_Python_Html_Css_Django_Mime - Fatal编程技术网

Python Django在自定义404页面上传输带有MIME类型text/html的样式表

Python Django在自定义404页面上传输带有MIME类型text/html的样式表,python,html,css,django,mime,Python,Html,Css,Django,Mime,我正在为我的网站设计一个定制的404页面,它是用Django构建的,但是我遇到了与404.html相关联的样式表的问题 为了查看自定义404页面,我在settings.py中设置了DEBUG=False,并将“*”添加到允许的_主机中 然后我用这行代码更改了urls.py: handler404 = 'site.views.custom_404' 在views.py中: def custom_404(request): return render_to_response("site/4

我正在为我的网站设计一个定制的404页面,它是用Django构建的,但是我遇到了与404.html相关联的样式表的问题

为了查看自定义404页面,我在settings.py中设置了DEBUG=False,并将“*”添加到允许的_主机中

然后我用这行代码更改了urls.py:

handler404 = 'site.views.custom_404'
在views.py中:

def custom_404(request):
    return render_to_response("site/404.html", context_instance=RequestContext(request))
现在,当我在本地获取404页面时,我看到了我在404.html中编写的html,但是样式表style.css不起作用。我在控制台中收到以下消息:

Resource interpreted as Stylesheet but transferred with MIME type text/html:
http://127.0.0.1:8000/site/static/style.css
除了这个新的404页面之外,我的站点上的所有其他页面仍然具有与style.css关联的适当样式

我尝试将其添加到settings.py,但没有任何更改:

if DEBUG:
    import mimetypes
    mimetypes.add_type("text/css", ".css", True)

您是否在settings.py中定义了静态文件

我猜你安装的应用程序中有“django.contrib.staticfiles”。 您可以这样定义它:

import os.path

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)
在url或设置中,执行以下操作:

custom_404 = "mysite.views.server_error"
在这方面:

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, "404.html")
    response.status_code = 404
    return response

是的,我一直使用静态URL来访问图像,但我尝试按照您的建议更改我的视图,现在我的样式表工作了,但我的所有图像都有404未发现错误**除了200(表示“OK”)之外,还有一些常见HTTP状态码的HttpResponse子类。您可以在文档中找到可用子类的完整列表。只需返回其中一个子类的实例,而不是正常的HttpResponse,即可指示错误**mor info in: