我有变量CHOICES和ListView。
CHOICES = [
('ALL', 'ALL'),
('IT', 'IT'),
('FINANCE', 'FINANCE'),
('OTHER', 'OTHER'),
]
class HomePageView(ListView):
model = Vacancy
template_name = 'vacancy_list/vacancy_list.html'
paginate_by = 10
page_kwarg = 'vacancy'
context_object_name = 'vacancies'
queryset = Vacancy.objects.order_by('-published_date')
如何将 CHOICES 添加到 html 标签?
我想替换<option>IT</option>
类似选择部门
完整代码
<div class="col-lg-3 col-md-4 col-xs-12">
<select name="q2" class="form-control" id="exampleFormControlSelect1">
{% if user.is_authenticated %}
<option>{{ user.department }}</option>
{% else %}
<option>ALL</option>
{% endif %}
<option>IT</option>
<option>Finance</option>
<option>Other</option>
</select>
</div>
我试过了
{% for department in CHOICES %}
<option value={{ department.0 }}>{{ department.1 }}</option>
{% endfor %}
但 CHOICES 为空
最佳答案
通常,您使用 ChoiceField
或TypedChoiceField
以 Django 形式,并在或多或少的程度上 <select>
当您在模板中呈现表单时,结构是自动的。
但是对于边缘情况,您确实可以将其传递到您的模板,正如 Tushortz 在他的回答中所示。
关于python - 将 django 变量添加到 <select> 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59643288/