[Answered ]-Django 404 Page not loading

2👍

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’s 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 better add handler404 contents above urls pattern. also don’t forget to change Debug=False in settings.py when testing in staging.

0👍

Try the view like that:

from django.views.generic.base import TemplateView

class Error404View(TemplateView):
    template_name = '404.html'

    def get(self, request, *args, **kwargs):
        response = super(Error404View, self).get(request, *args, **kwargs)
        response.status_code = 404
        return response

    @classmethod
    def as_error_view(cls):
        v = cls.as_view()
        def view(request):
            r = v(request)
            r.render()
            return r
        return view

Than, in urls.py:

handler404 = Error404View.as_error_view()

Leave a comment