[英]Strange behavior in select drop-down in a Django template
I'm working on two linked form fields ( Class
and Students
) where the user selects a class from the drop-down menu and then the students form field updates with the corresponding list of students. 我正在研究两个链接的表单字段(“ Class
和“ Students
),其中用户从下拉菜单中选择一个班级,然后“学生表单”字段会使用相应的学生列表进行更新。
I have it all working with the AJAX logic, except...except...I have run into some strange behavior when trying to apply the selected
attribute to the <option>
tags. 我已经全部使用AJAX逻辑了,除了...例外...在尝试将selected
属性应用于<option>
标记时遇到了一些奇怪的行为。
views.py views.py
def load_students(request):
classid = request.GET.get('classid')
contractid = request.GET.get('contractid')
# Lookup students for given class
students = Student.objects.getclass(classid=classid)
if(contractid):
# Generate list of students associated with this contract
contract_party_list = []
contract_parties = ContractParty.objects.get_contract_parties(
contractid=contractid
)
for mycontractparty in contract_parties:
contract_party_list.append(mycontractparty.partyuserid)
# Generate new student list (with appended contract user info)
student_list = []
for mystudent in students:
# Set flag to determine whether student is part of contract
if(mystudent.studentuserid in contract_party_list):
selectedFlag = True
else:
selectedFlag = False
# Add updated student info to new student list
student_list.append(
{
'studentuserid':mystudent.studentuserid,
'firstname':mystudent.firstname,
'lastname':mystudent.lastname,
'selectedFlag': selectedFlag
}
)
students = student_list
return render(request, 'dropdown_ajax.html', {'students': students})
dropdown_ajax.html dropdown_ajax.html
{% if students %}
{% for student in students %}
<option
value="{{ student.studentuserid }}"
{% if student.selectedFlag %} selected="selected"{% endif %}
>
{{ student.firstname }} {{ student.lastname }}
</option>
{% endfor %}
{% endif %}
This line is causing me problems: {% if student.selectedFlag %} selected="selected"{% endif %}
此行导致我遇到问题: {% if student.selectedFlag %} selected="selected"{% endif %}
The strange behavior is that the "selected" attribute never gets applied, even though student.selectedFlag
evaluates to True. 奇怪的行为是,即使student.selectedFlag
评估为True,也不会应用“ selected”属性。
A couple things I tried: 我尝试了几件事:
I moved the line above outside of the tag to see what it would do. 我将行移到了标签之外,以查看它会做什么。 It displays the text "selected" for the correct entries. 它显示正确选择的文本“已选择”。
I replaced if student.selectedFlag
with if student.studentuserid == 1
and the correct student was selected in the field. 我将if student.selectedFlag
替换为if student.studentuserid == 1
并在该字段中选择了正确的学生。
I passed "True" / "False and numeric values for selectedFlag
instead of Boolean. I tried if student.selectedFlag == "True"
. Nothing. 我为selectedFlag
传递了“ True” /“ False和数值”,而不是布尔值。我尝试了if student.selectedFlag == "True"
。
I'm not sure what's causing this behavior. 我不确定是什么原因导致了这种行为。 My guess it's something to do with the Django Boolean variables not being evaluated correctly within the <option>
fields. 我的猜测是与<option>
字段中未正确评估Django布尔变量有关。
I don't think this is a django issue - it's an issue with the html. 我不认为这是Django的问题-这是html的问题。 The html attribute should be treated as a boolean (present/not present), not given the value "selected". html属性应被视为布尔值(存在/不存在),而不是给定值“ selected”。 The correctly rendered html should look something like this: 正确呈现的html应该如下所示:
<option value="value" selected>...</option>
not this, which I think is what's happening in yours: 不是这个,我想这就是你正在发生的事情:
<option value="value" selected="selected">...</option>
So the corrected code would looks like this: 因此,更正后的代码将如下所示:
dropdown_ajax.html dropdown_ajax.html
{% if students %}
{% for student in students %}
<option
value="{{ student.studentuserid }}"
{% if student.selectedFlag %} selected{% endif %}
>
{{ student.firstname }} {{ student.lastname }}
</option>
{% endfor %}
{% endif %}
As usual...the solution manifests itself after taking a break :) It's official...I'm an idiot. 像往常一样...该解决方案在休息后表现出来:)这是官方的...我是个白痴。 In my testing, I was looking at two different scenarios. 在测试中,我正在研究两种不同的情况。 The one that was giving me the "weird behavior", I wasn't passing the contractid
value. 这给了我“怪异的行为”,我没有传递contractid
价值。 So it was never getting to the logic that sets the selected
options. 因此,它永远不会涉及设置selected
选项的逻辑。 D'oh! 天哪! Thanks for the tips. 感谢您的提示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.