views:

38

answers:

2

Hi guys,

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.

+1  A: 

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.

Manoj Govindan
Right now I've two options but in some near future I would have a least one more view pointing to the same template. And I don't know why but your example doesn't work, now I've only a button but the direction is always the same, colors.
Pat
Can you print the value of `back_to` and see what comes up? Also, if `colors` is not a variable then you should have quotes around it. Like this: `{% ifequal back_to "colors" %}`
Manoj Govindan
nice tip! if I put colors inside quotes nothing appears. when I tried to print back_to ({{ back_to }}) nothing appears. So the mistake should be there..
Pat
Also keep in mind the `pprint` filter.
Manoj Govindan
Thanks!Actually it was a really really silly mistake. I was passing in the wrong view. Sorry about that..I'm still learning and sometimes silly mistakes happens..
Pat
A: 

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.

Deniz Dogan
Before I tried with urls but I didn't work. I can show what I've done:{% url /browse/colors/ as col_url %}{% url /browse/cars/ as car_url %}<form action=""> {% if col_url %} <a href="{{ col_url }}" style= "text-decoration: none"> <input type="button" value="Go back"></input></a> {% endif %} {% if car_url %} <a href="{{ car_url }}" style= "text-decoration: none"> <input type="button" value="Go back"></input></a> {% endif %}</form>
Pat