Cha*_*rts 3 django django-templates
我有一个用户模型,并且用户具有大学属性。我希望用户能够滚动浏览大学列表并选择一所大学作为按钮,我不想有下拉选择器。UserInfo.UNIVERSITY_CHOICES
因此,我将(UserInfo
与用户模型一对一)传递给我的模板,并尝试使用以下方法迭代它:
<form method="get" action="/newUniversity/">
{% csrf_token %}
{% for school in universityList %}
<input class='submitbtn' type="submit" name="school" value="{{ school }}"></center>
{% endfor %}
</form>
Run Code Online (Sandbox Code Playgroud)
然而,我得到了一堆按钮,上面写着的('Harvard','Harvard')
不仅仅是哈佛。我尝试通过这样做来索引元组,school[1]
但这给了我一个错误。有没有办法做到这一点,或者我的做法完全错误?
如果UserInfo.UNIVERSITY_CHOICES
是元组(就像您在 django admin 中使用的元组),您必须选择您要使用的值:
school.0
Run Code Online (Sandbox Code Playgroud)
或者
school.1
Run Code Online (Sandbox Code Playgroud)
注意 Django 的模板语言不是 Python!您可以在此处查看该语言的参考: https: //docs.djangoproject.com/en/dev/topics/templates/
归档时间: |
|
查看次数: |
2882 次 |
最近记录: |