tags:

views:

632

answers:

2

I have an urls.py with this line:

url(r'^logout/$', 'django.contrib.auth.views.logout', name="auth_logout"),

In my template tag i have this line:

<a href="{% url auth_logout %}">Logout</a>

Now, I would like to add the next_page param to the url templatetag, but I can't get it to work. I have tried this:

{% url auth_logout request.path %}"

...and this:

{% url auth_logout request,request.path %}

But none of them works. How can I provide the function with the optional next_page paramter usinging the url templatetag?

Thanks!

+4  A: 

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
That almost does the trick! Having two lines in urls.py solves it, but brings up the django default loutout page when just entering /logout/.Adding {'next_page': '/'}as you suggest causes the redirect to go / in all cases, even when next is specified. Viewing the source code for logout explains that behaviour.So it appears, the parameterized one is used by the template tag, but the hard-coded on is used when the link is actually clicked?
Björn Lilja
+1  A: 

For what it's worth, I use this:

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