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和contest,我希望通过html页面的下拉菜单输入这两个字段。现在问题在于forms.html。我无法使用下拉菜单填充表单字段。当我使用{{form.ins}}和{{form.contest}}时,它会起作用(接收在文本框中手动输入的数据并将正确的页面重定向到),但它需要以文本框的形式输入。 我无法将下拉菜单与我的django表单相关联。如果我在forms.html中提供{{form.ins}}以及下拉菜单,则会出现文本框输入和下拉菜单,其中只有文本框正常运行,仍然没有使用下拉列表。我希望通过下拉菜单接收表单输入,并且hmtl页面中不应该输入文本框。
答案 0 :(得分:0)
在您的表单中使用form.ChoiceField而不是CharField()。
from django import forms
INS_CHOICES = (
('NIT', 'Netaji Subhas Institute of Technology, New Delhi'),
('NSIT', 'Netaji Subhas Institute of Technology, New Delhi'),
...
)
class DataForm(forms.Form):
ins = forms.ChoiceField(choices=INS_CHOICES)
有关更多信息,请查看此处:https://docs.djangoproject.com/en/dev/ref/forms/fields/#choicefield
但是你应该考虑使用CreateView,因为它的工作量会少很多:https://docs.djangoproject.com/en/dev/ref/class-based-views/generic-editing/