简体   繁体   English

Django下拉菜单问题

[英]Issue on dropdown menu with Django

I have an html page where the designation of employees can be selected via a drop down menu. 我有一个html页面,可通过下拉菜单选择雇员的指定。 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. 下拉菜单中的选项为“选择一个名称”,“学员”,“软件工程师”,“项目经理”,“测试人员”等。即使根据指定正确列出了员工,下拉菜单中的选项始终保留“选择名称”(这是下拉菜单中的第一个选项)。请提供帮助:* MY *很抱歉,缩进效果很差,我无法在此处输入html代码。 HTML IS : HTML是:

<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: 我对Django的看法是:

def filter(request):
    newData = EmployeeDetails.objects.filter(designation=request.POST.get('designation'))    
    return render_to_response('filter.html',{'newData':newData})

As far as I can understand your question, you are having a problem with rendering the correct value for Designation. 据我能理解您的问题,您在呈现正确的Designation值时遇到问题。 You need to fix the errors in your code first. 您需要先修复代码中的错误。 Why is selected=True inside the select tag? 为什么在select标签中选择了select selected=True 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. 应该可以工作,其中VAL对应于您要选择的选项的值。 However, you will need to do this for all < option >s, so I agree with Haes, django forms is still the way to go. 但是,您将需要对所有<选项>执行此操作,因此我同意Haes的观点,仍然需要使用django表单。

I suggest you make use of Django Forms . 我建议您使用Django Forms Django Forms will take care of setting initial values, form validation, rendering the form and more. Django Forms将负责设置初始值,表单验证,呈现表单等等。

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 . 附带说明一下,如果您只使用这种形式过滤某些数据,则最好使用GET请求(而不是POST),如此处所讨论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM