rv_k
rv_k

Reputation: 2403

Issue on dropdown menu with Django

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})

Upvotes: 0

Views: 1876

Answers (2)

gladysbixly
gladysbixly

Reputation: 2659

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.

Upvotes: 1

Haes
Haes

Reputation: 13116

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.

Upvotes: 0

Related Questions