小编ani*_*k_s的帖子

django 模板中的自定义计数器

我在 django 模板页面中有这段代码

<select class="selectpicker datatable-column-control" multiple
{% for q_group in question_groups %}
    <optgroup label="{{ q_group.name }}">
    {% for q in  q_group.questions %}
        <option value="{{ forloop.counter0 }}">{{ q.title }}</option>
    {% endfor %}
    </optgroup>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

我想要每个选项标签的值在每次迭代中都会增加。如果我有 10 个选项标签,那么它们的值将从 0 到 9。这 forloop.counter0不能满足我的需求,因为当外循环完成一次时,内循环计数器初始化为 0。

python django django-templates

4
推荐指数
1
解决办法
7900
查看次数

仅适用于特定应用程序的上下文处理器

'context_processors': [
    ...
    ...
    "publicfront.views.context_processors.add_event_url"
 ],
Run Code Online (Sandbox Code Playgroud)

我在 settings.py 中添加了这个上下文处理器,并且只想用于特定的应用程序。我怎么能做到这一点?

python django

3
推荐指数
1
解决办法
874
查看次数

标签 统计

django ×2

python ×2

django-templates ×1