I'm designing a custom 404 page for my site which is built with Django, but I'm running into problems with the stylesheet associated with 404.html.
I set DEBUG = False in settings.py in order to see my custom 404 page, and added '*' to ALLOWED_HOSTS.
Then I changed urls.py with this line:
handler404 = 'site.views.custom_404'
In views.py:
def custom_404(request):
return render_to_response("site/404.html", context_instance=RequestContext(request))
Now when I get the 404 page locally, I see my html that I wrote in 404.html, but the stylesheet, style.css, doesn't work. I get this message in the console:
Resource interpreted as Stylesheet but transferred with MIME type text/html:
http://127.0.0.1:8000/site/static/style.css
All the other pages on my site still have the proper style associated with style.css, except this new 404 page.
I tried adding this to settings.py, but nothing changed:
if DEBUG:
import mimetypes
mimetypes.add_type("text/css", ".css", True)
Have you defined your static files in settings.py ?
I'm guessing you have 'django.contrib.staticfiles', in your installed apps. you can define it like this :
import os.path
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
)
else in your url or settings do this :
custom_404 = "mysite.views.server_error"
and in views this :
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.