代码之家  ›  专栏  ›  技术社区  ›  psoares

django模板html

  •  1
  • psoares  · 技术社区  · 14 年前

    我有两种不同的观点(例如,一种是颜色,另一种是汽车)

    <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>   
    

    在视图颜色中,我传递'back\u to':'colors'和视图汽车'back\u to':'cars'。
    结果是,我有两个按钮返回到这两个页面。
    我想要的是如果我在彩色页面,只有按钮返回到我选择颜色的页面,如果我在汽车页面,只有按钮返回到我选择汽车的页面。

    2 回复  |  直到 14 年前
        1
  •  1
  •   Community CDub    8 年前

    如果您可以保证只有两个选项(汽车或颜色),则可以执行以下操作:

    <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>   
    

    <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>   
    

    更新

    good point 关于使用反转。

        2
  •  0
  •   Deniz Dogan    14 年前

    而不是将普通字符串作为 back_to django.core.urlresolvers.reverse 像这样的:

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

    'name_of_view' name of your view 在URLconf中设置。希望有帮助。