Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django template tag: How to send next_page in {url auth_logout}?

Tags:

django

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!

like image 501
Björn Lilja Avatar asked Aug 18 '09 21:08

Björn Lilja


People also ask

What does the built in Django template tag URL do?

A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template.

What does {% URL link %} do?

{% url 'contact-form' %} is a way to add a link to another one of your pages in the template. url tells the template to look in the URLs.py file.

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.

What is URL tag in Django?

The URL template tag is a typical type of Tag in the Django Template Language framework. This tag is specifically used to add View URLs in the Template Files. In the HTML Template file, URL tags are used with the Anchor, <a href> attribute of HTML, which handles all the URLs in HTML.


2 Answers

For what it's worth, I use this:

<a href="{% url auth_logout %}?next=/">Logout</a>
like image 58
Sander Smits Avatar answered Oct 22 '22 12:10

Sander Smits


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>
  • Docs on logout
  • Docs on url templatetag
like image 29
hughdbrown Avatar answered Oct 22 '22 13:10

hughdbrown