Skip to content Skip to sidebar Skip to footer

How To Redirect 404 Requests To Homepage In Django Single Page App Using Nginx?

I have a django single page application. Currently when you visit a url on the site that doesn't exist a 404 error is displayed. However, in this case I want to redirect to the hom

Solution 1:

First, create a view to handle all 404 requests.

# views.pyfrom django.shortcuts import redirect

defview_404(request, exception=None):
    # make a redirect to homepage# you can use the name of url or just the plain linkreturn redirect('/') # or redirect('name-of-index-url')

Second, put the following in your project's urls.py:

handler404 = 'myapp.views.view_404'# replace `myapp` with your app's name where the above view is located

Solution 2:

404 page Error handling

->set DEBUG=False in settings.py

-> handler404='appname.views.view_404' add this line at the bottom of the root urls.py file.

->add this function to the views file of the app which is mentioned in handler404

defview_404(request,exception=None)
    return redirect('redirect-url-name')

Post a Comment for "How To Redirect 404 Requests To Homepage In Django Single Page App Using Nginx?"