psoares
psoares

Reputation: 4883

django templates html

I've two different views (for instance, one for colors and other for cars ) That views point to the same template.
If you click in one color, the template will show all the information about the color selected, same thing whit the car.

What I'm trying to do is to insert a button to go back:

<form action="">
{% ifequal back_to colors %}
    <a href="/browse/colors/" style= "text-decoration: none">
    <input type="button" value="Go back"></input></a>
{% endifequal %}  
{% ifequal back_to cars %}
    <a href="/browse/cars" style= "text-decoration: none">
    <input type="button" value="Go back"></input></a>
{% endifequal %}
</form>   

where in the view colors I pass 'back_to': 'colors' and view cars 'back_to':'cars'.
The results is that I have two buttons to go back in both pages.
What I wanted was if I was in color page, only the button to go back to page where I select colors, and if I was in car page, only the button to go back to the page I select cars.
Hope I made my point, if someone how to do this, I'll be grateful.

Upvotes: 1

Views: 207

Answers (2)

Deniz Dogan
Deniz Dogan

Reputation: 26227

Instead of passing normal strings as values for back_to you could make them into URLs using django.core.urlresolvers.reverse as such:

from django.core.urlresolvers import reverse
url_to_my_view = reverse('name_of_view') # Gets the URL to the view!

'name_of_view' is the name of your view as set in your URLconf. Hope that helps.

Upvotes: 0

Manoj Govindan
Manoj Govindan

Reputation: 74695

If you can guarantee that there are only two options (cars or colors) then you can do the following:

<form action="">
{% ifequal back_to colors %}
    <a href="/browse/colors/" style= "text-decoration: none">
    <input type="button" value="Go back"></input></a>
{% else %}
    <a href="/browse/cars" style= "text-decoration: none">
    <input type="button" value="Go back"></input></a>
{% endifequal %}
</form>   

Also the above snippet can be simplified to:

<form action="">
<a href="{% ifequal back_to colors %}/browse/colors{% else %}/browse/cars{% endifequal %}" 
   style= "text-decoration: none">
    <input type="button" value="Go back"></input></a>
</form>   

Update

Deniz Dogan makes a good point about using reverse.

Upvotes: 1

Related Questions