简体   繁体   English

django模板html

[英]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'. 在视图颜色中,我传递'back_to':'colors'并查看汽车'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. 希望我能指出我的意思,如果有人能做到这一点,我将不胜感激。

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. Deniz Dogan 很好地介绍了如何使用反向。

Instead of passing normal strings as values for back_to you could make them into URLs using django.core.urlresolvers.reverse as such: 您可以像使用django.core.urlresolvers.reverse那样将常规字符串作为back_to值传递给URL,而不是:

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. 'name_of_view'在URLconf中设置的视图名称 Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM