Populate django form using dropdown menu for two fields

Sunday, July 6, 2014

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="INSTITUTE">select contest Name</label>
<select id="disabledSelect" class="form-control">
<option>select Contest</option>
<option>JULY LONG</option>
<option>JULY SHORT</option>
<option>JUNE LONG</option>
<option>JUNE SHORT</option>
<option>MAY LONG</option>
<option>MAY SHORT</option>
<option>APRIL LONG</option>
<option>APRIL SHORT</option>
<option>MARCH LONG</option>
<option>MARCH SHORT</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>


There are two fields in forms.py : ins and contest, both of which I wish to enter through dropdown menu in the html page. Now the problem lies in the forms.html. I am not able to populate form fields using dropdown menu. When I use {{ form.ins }} and {{ form.contest }} it works(Data entered manually in textbox is received and correct page is redirected to), but it takes input in the form of textbox. I am not able to link dropdown menu with my django form. If I give {{ form.ins }} along with dropdown menu in forms.html then there appears both textbox input and dropdown menu of which only textbox functions properly, there remains no use of dropdown. I wish to recieve form input through dropdown menu and there should be no textbox input in hmtl page.



</form>






http://ift.tt/1oy5pdr