【问题标题】:Concatenate strings together in template if statement在模板 if 语句中将字符串连接在一起
【发布时间】:2016-12-03 19:26:23
【问题描述】:

我阅读了this,我的代码如下所示:

html:

<li {% if request.path == '/groups/{{ group.id }}/' %}class="active"{% endif %} ><a href="{% url 'detail' group.id %}">Link</a></li>

唯一的问题是/groups/{{ group.id }}/明显变成了:

/groups/{{ group.id }}/

不是

/groups/1/

that 如果为页面上的其他 10 个链接键入它,最终会变成很多代码。

【问题讨论】:

    标签: python django


    【解决方案1】:

    不要硬编码网址,而是使用url标签和as

    {% url 'my_group_url_name' group.id as group_url %}
    {% if request.path == group_url %}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-18
      • 2011-11-12
      • 2017-01-15
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 2014-06-18
      相关资源
      最近更新 更多