0
我嘗試在Django中創建一個下拉菜單,但輸出中沒有這樣的菜單。如何在Django中創建一個下拉菜單?
這裏是我的view.py:
class MajorProgramForm(forms.Form):
major_programs = forms.ChoiceField(label="Major Programs", choices=MAJORS, required=True)
def choose_major_program(request):
if request.method == "GET":
form = MajorProgramForm(request.GET)
context["form"] = form
return render(request, "program.html", context)
這裏是我的template.py:
<html>
<head>
<title>Choose Your Courses</title>
</head>
<body>
<div id="header">
<h1>Choose Your Courses</h1>
</div>
<div class="frame">
<form name="form", action="{% url 'choose_major_program' %}" method="GET">
{% csrf_token %}
<ul>
{% for option in form.major_programs.choices %}
<li>{{ option.name }}</li>
{% endear %}
</ul>
<input type="submit" value="Submit">
</form>
</div>
</body>
</html>
下面是urls.py:
urlpatterns = [
url(r'^$', choose_major_program, name="choose_major_program")]
而結果是這樣的: A web page
謝謝!
謝謝你的建議!編輯我的代碼後,輸出中有一個下拉框,但下拉框是空的。 –