【发布时间】:2014-07-06 09:50:42
【问题描述】:
forms.py
from django import forms
class DataForm(forms.Form):
ins = forms.CharField(max_length=100)
contest = forms.CharField(max_length=100)
views.py
from django.shortcuts import render_to_response, render
from django.http import HttpResponseRedirect
from django.contrib import messages
from django.core.context_processors import csrf
import mylife
import codechef
from codechef.models import chef, aprilcook
from forms import DataForm
def all_users(request):
return render_to_response('C:/Users/shubham/Desktop/code_chef/project/mylife/codechef/templates/allusers.html', { 'user_list': chef.objects.all() })
def print_table(request):
return render_to_response('C:/Users/shubham/Desktop/code_chef/project/mylife/codechef/templates/tables.html')
def display(request):
if request.method == 'POST':
form = DataForm(request.POST)
if form.is_valid():
print '-----------------------------------'
print form
print '-----------------------------------'
insti = form['ins'].value()
return render_to_response('C:/Users/shubham/Desktop/code_chef/project/mylife/codechef/templates/allusers.html', { 'user_list': chef.objects.filter(inst = insti) })
else:
form = DataForm() # An unbound form
return render(request, 'forms.html', {
'form': form,
})
def enter_data(request):
form = DataForm(request.POST)
return render(request, 'forms.html', {
'form': form,
})
forms.html
<form action="/display/" method="post">{% csrf_token %}
<div class="form-group">
<label for="id_ins">select Institute Name</label>
<select id="id_ins" class="form-control" size="1">
<option>select Institute</option>
<option value="National Institute of Technology, Kurukshetra">National Institute of Technology, Kurukshetra</option>
<option value="Netaji Subhas Institute of Technology, New Delhi">Netaji Subhas Institute of Technology, New Delhi</option>
<option value="Maulana Azad National Institute of Technology, Bhopal">Maulana Azad National Institute of Technology, Bhopal</option>
<option value="Birla Institute of Technology Mesra">Birla Institute of Technology Mesra</option>
<option value="Delhi Technological University">Delhi Technological University</option>
<option value="Shiraz University">Shiraz University</option>
<option value="Indian Institute of Information Technology, Allahabad">Indian Institute of Information Technology, Allahabad</option>
<option value="Indian Institute of Technology Guwahati">Indian Institute of Technology Guwahati</option>
<option value="National Institute of Technology, Kurukshetra">National Institute of Technology, Kurukshetra</option>
<option value="National Institute of Technology Tiruchirappalli">National Institute of Technology Tiruchirappalli</option>
</select>
</div>
<div class="form-group">
<label for="id_contest">select contest Name</label>
<select id="disabledSelect" class="form-control">
<option>select Contest</option>
<option value="JULY LONG">JULY LONG</option>
</select>
</div>
<div class="checkbox">
<label>
<input type="checkbox">Agree terms
</label>
</div>
<button type="submit" value="Submit" class="btn btn-primary">Submit</button>
forms.py 中有两个字段:ins 和竞赛,我希望通过 html 页面中的下拉菜单进入这两个字段。现在问题在于forms.html。我无法使用下拉菜单填充表单字段。当我使用 {{ form.ins }} 和 {{ form.contest }} 时,它可以工作(接收到在文本框中手动输入的数据并将正确的页面重定向到),但它以文本框的形式输入。 我无法将下拉菜单与我的 django 表单链接。如果我在 forms.html 中提供 {{ form.ins }} 和下拉菜单,那么会同时出现文本框输入和下拉菜单,其中只有文本框正常工作,下拉菜单仍然没有使用。我希望通过下拉菜单接收表单输入,并且 hmtl 页面中应该没有文本框输入。
【问题讨论】:
标签: django forms drop-down-menu