[Fixed]-Django template tag: How to send next_page in {url auth_logout}?

26πŸ‘

βœ…

Two things:

  1. django.contrib.auth.views.logout() takes an optional next_page which you are not providing
  2. url templatetag has comma-separated arguments

So, first modify your url to accept next_page
Your URLConf needs modification to pass in the next page, something like this for a hard-coded redirect:

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

and one parameterized result:

url(r'^logout/(?P<next_page>.*)/$', 'django.contrib.auth.views.logout', name='auth_logout_next'), 

And then modify your template to pass in the next_page

<a href="{% url auth_logout_next /some/location %}">Logout</a>
πŸ‘€hughdbrown

28πŸ‘

For what it’s worth, I use this:

<a href="{% url auth_logout %}?next=/">Logout</a>
πŸ‘€Sander Smits

9πŸ‘

Sander Smits has the most easy solution. In your case use:

<a href="{% url "auth_logout" %}?next={{ request.path|urlencode }}">Logout</a>

And in a more general case, use:

<a href="{% url "auth_logout" %}?next={% url "my_url" my_params |urlencode %}">Logout</a>
πŸ‘€Wim Feijen

0πŸ‘

If anyone wants to give out an HTML link through views.py validation error with mark_safe and translations.

I used this part to logout the user and then send him to the password recorve page.

mark_safe(_("Error message") + '<a href="' + reverse('logout') + '?next=' + reverse('password_recover') + '">' + ugettext("Forgot password?") + '</a>')
πŸ‘€Dude

Leave a comment