我的HTML代码
<select class="custom-select" id="lab" name="lab" onchange="toggleLabs(this);" required >
<option value="no">No Lab Choosen</option>
<option value="newlab" id="newlab">New Lab</option>
{% for lab in all_obj %}
<option value='{{lab.lab_name}}' onclick="myFunc()">{{lab.lab_name}}</option>
{% endfor %}
</select>
javascript: 我正在访问所选选项并将其传递给views.py
function toggleLabs(select){
iden_lab=select.options[select.selectedIndex].text;
$(document).ready(function () {
$.get("", url:'/listlabs(idenlab)',{slab: iden_lab}, function (data) {
alert(iden_lab);
});
});
我的views.py将其渲染回原处
def listlabs(request,):
all_obj = labs.objects.all()
val = labs.objects.all(lab_name)
if request.method == 'POST':
form = create_lab()
if request.is_ajax():
lab_selected = request.GET['slab']
obj_val = labs.objects.filter(lab_name=lab_selected).values('lab_name')[0]
print(obj_val)
val = obj_val['lab_name']
print(val)
return render(request, 'listlabs.html/', {'form': form, 'all_obj': all_obj, 'val': val})
else:
form = create_lab(request.POST)
return HttpResponse('success')
return render(request, 'listlabs.html/', {'form': form ,'all_obj': all_obj,'val': val})
我希望val
仅在我进入If
部分时才渲染。
我尝试打印以检查它是否在if块内。
它在我的命令提示符中打印了该值,但未呈现return语句。
仅呈现最后一个return语句,其中我的val
未被覆盖
该如何解决?为什么我的退货单不起作用?
谢谢。
答案 0 :(得分:0)
尝试
def listlabs(request,):
all_obj = labs.objects.all()
val = labs.objects.all(lab_name)
form = create_lab()
if request.is_ajax():
lab_selected = request.GET['slab']
obj_val = labs.objects.filter(lab_name=lab_selected).values('lab_name')[0]
print(obj_val)
val = obj_val['lab_name']
print(val)
return render(request, 'listlabs.html/', {'form': form, 'all_obj': all_obj, 'val': val})
else:
form = create_lab(request.POST)
return HttpResponse('success')
return render(request, 'listlabs.html/', {'form': form ,'all_obj': all_obj,'val': val})