Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django templates: create a "back" link?

I'm tooling around with Django and I'm wondering if there is a simple way to create a "back" link to the previous page using the template system.

I figure that in the worst case I can get this information from the request object in the view function, and pass it along to the template rendering method, but I'm hoping I can avoid all this boilerplate code somehow.

I've checked the Django template docs and I haven't seen anything that mentions this explicitly.

like image 815
TM. Avatar asked Feb 08 '09 02:02

TM.


People also ask

How do I create a link in Django?

Just use the same label {% url 'index' %} . You may use each name in urls.py to link to the url. Show activity on this post. Create a new URL in the same format and give that name instead of index.

How can I get previous URL in Django?

You can do that by using request. META['HTTP_REFERER'] , but it will exist if only your tab previous page was from your website, else there will be no HTTP_REFERER in META dict . So be careful and make sure that you are using . get() notation instead.

How do I redirect a back page in Django?

In Django, redirection is accomplished using the 'redirect' method. The 'redirect' method takes as argument: The URL you want to be redirected to as string A view's name. In the above example, first we imported redirect from django.

How do I reuse a Django template?

To reuse a Django template you use the Django built-in {% extends %} tag. The {% extends %} tag uses the syntax {% extends <name> %} to reuse the layout of another template. This means that in order to reuse the layout in listing 3-10 defined in a file base. html , you use the syntax {% extends "base.


1 Answers

Actually it's go(-1).

<input type=button value="Previous Page" onClick="javascript:history.go(-1);"> 
like image 97
mcwong Avatar answered Oct 07 '22 22:10

mcwong