How to specify a custom 404 view for Django using Class Based Views?
Never mind, I forgot to try this:
from path.to.view import Custom404handler404 = Custom404.as_view()
Seems so simple now, it probably doesn't merit a question on StackOverflow.
Managed to make it work by having the following code in my custom 404 CBV (found it on other StackOverflow post: Django handler500 as a Class Based View)
from django.views.generic import TemplateViewclass NotFoundView(TemplateView): template_name = "errors/404.html" @classmethod def get_rendered_view(cls): as_view_fn = cls.as_view() def view_fn(request): response = as_view_fn(request) # this is what was missing before response.render() return response return view_fn
In my root URLConf file I have the following:
from apps.errors.views.notfound import NotFoundViewhandler404 = NotFoundView.get_rendered_view()
In your main urls.py
you can just add from app_name.views import Custom404
and then set handler404 = Custom404.as_view()
. It should work