[Django]-Django unable to render logout template

5👍

You are using contrib.auth‘s logout view in your urls.py. This view redirects to the URL specified by 'next_page'. You provide '/account/logout' as next_page — where again the logout view is called!

That leads to an (infinite) redirect loop: the view redirects to itself.

Try instead: in your own logout view:

# no redirecting here!
return render(request, 'account/logout.html', 
              context_instance=RequestContext(request))

Add a url for that view in account/urls.py:

url(r'^post-logout/$', logout, name='post-logout'), 
# logout being your own view

Then provide that url as 'next_page' to the actual (auth) logout:

url(r'^logout/$', 'django.contrib.auth.views.logout',
    {'next_page': '/account/post-logout'},  name='logout'),

7👍

I was having same issue, but then I kept reading

“If you are seeing the log out page of the Django administration site instead of your own log out page, check the INSTALLED_APPS setting of your project and make sure that django.contrib.admin comes after the account application. Both templates are located in the same relative path and the Django template loader will use the first one it finds.”

0👍

The solution to the problem is to map the url as follows

url(r’^logout/$’, ‘django.contrib.auth.views.logout’, { ‘template_name’: ‘account/logout.html’,}, name=’logout’ ),

Leave a comment