0

I have an html page where the designation of employees can be selected via a drop down menu. And the selected option in the drop down menu displays employees of that corresponding designation. I am able to display the employees on selecting the designation from drop-down menu. The choices in drop-down are 'Select A Designation','Trainee','Software Engineer','Project Manager','Tester' etc. Even though the employees are listed correctly based on designation, the option in drop down always remains 'Select a designation' (which is the first option in drop-down).Please help: *MY * Sorry for the poor indentation, i couldn't find how to enter html code here. HTML IS :

<form action="http://10.1.0.90:8080/filter/" method="POST">  
Designation:<select name="designation" onchange="document.forms[0].submit()" selected="True">
 <option value="" selected>Select A Designation</option>  
 <option value="Trainee">Trainee</option>  
 <option value="Software Engineer Trainee">Software Engineer Trainee</option>  
 <option value="Software Engineer">Software Engineer</option >  
 <option value="Senior Software Engineer">Senior Software Engineer</option>  
 <option value="Project Manager">Project Manager</option>  
 <option value="System Administrator">System Administrator</option> 
 <option value="Tester">Tester</option>   
</select>

My views of Django is:

def filter(request):
    newData = EmployeeDetails.objects.filter(designation=request.POST.get('designation'))    
    return render_to_response('filter.html',{'newData':newData})
YOU
  • 120,166
  • 34
  • 186
  • 219
rv_k
  • 2,383
  • 7
  • 39
  • 52

2 Answers2

1

As far as I can understand your question, you are having a problem with rendering the correct value for Designation. You need to fix the errors in your code first. Why is selected=True inside the select tag? And you have a hanging 'selected' inside the first option tag. Something like:

Designation:
    <select name="designation" onchange="document.forms[0].submit()">
        <option value="" {% ifequal VAL this_value %}selected="True"{% endif %}>Select A Designation</option>

should work, where VAL corresponds to the value of the option that you want to be selected. However, you will need to do this for all < option >s, so I agree with Haes, django forms is still the way to go.

gladysbixly
  • 2,591
  • 18
  • 15
0

I suggest you make use of Django Forms. Django Forms will take care of setting initial values, form validation, rendering the form and more.

A side note, if you just filter some data with this form, you better use a GET request (instead of POST), as it is discussed here.

Community
  • 1
  • 1
Haes
  • 12,891
  • 11
  • 46
  • 50