Django custom 404 page not working

2019-06-22 01:53发布

So, in my projects I wanted to add a custom 404 error page, so I followed what I could find on the web, but nothing seemed to work for me.

This is what I have on my files:

settings.py

import os
# Django settings for HogwartsMail project.

PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))

DEBUG = False
TEMPLATE_DEBUG = DEBUG

# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
ALLOWED_HOSTS = ["*"]

urls.py

urlpatterns = patterns('',
    ...
)

handler404 = "HogwartsMail.views.error404"

views.py

def error404(request):
    return render(request,'404.html')

Whenever I try to enter a random url, though, instead of getting a 404 error, I get a Server Error (500).

As I've said, i tried it many different ways, but none actually worked for me.

Also, another problem I am having is that the pages I have load really slowly and they don't open the styling or images, so I assume that, when DEBUG = False, I need everything to be already online. Is that correct?

5条回答
时光不老,我们不散
2楼-- · 2019-06-22 02:10
def error404(request):
    return HttpResponseNotFound(render_to_string('404.html'))

as another example:

def error404(request):
    return render(request,'404.html', status=404)
查看更多
ゆ 、 Hurt°
3楼-- · 2019-06-22 02:11

As a best practice I've add 2 urls.py in my project. So I add handler404 in my base urls.py. my parent urls.py is look like this

from django.conf.urls import handler404
import myapp

urlpatterns = [
   path('', include('myapp.urls')),
   path('admin/', admin.site.urls),
]

handler404 = myapp.views.error_404

and error_404 is a view in my views.py. like this

def error_404(request, exception):
    data = {"name": "somthing error"}
    return render(request,'myapp/404.html', data)

check the full tutorial here

查看更多
Viruses.
4楼-- · 2019-06-22 02:15

In the urls.py of project folder add this where the accounts (it can be any app) is an app of the same project:

project/urls.py

handler404 = 'accounts.views.page_not_found'

In accounts' view add this:

accounts/views.py

def page_not_found(request):

"""
Page not found Error 404
"""

     response = render_to_response('404.html',context_instance=RequestContext(request))
     response.status_code = 404
     return response

Don't forget to make necessary imports. Also don't forget to change Debug=False in settings.py when testing in staging.

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-06-22 02:19

Ok, it turns out I don't seem to need to do anything but create a 404.html page. By creating in, when an "Page not found" error is raised, Django will first look for a 404.html in your templates, before using it's own error page. I still have a problem with my static files not being on the web - when set DEBUG = False neither the css file nor any of my images appear. Other than that, my problem as fixed.

查看更多
Juvenile、少年°
6楼-- · 2019-06-22 02:32

When DEBUG = False, you can use python manage.py runserver --insecure to serve the static files during local development.

See more information in this answer.

查看更多
登录 后发表回答