更改下拉菜单时,我需要在views.py中获取“ cc_value”值,而无需单击提交按钮。 我提到那里的博客很少,他们提到我们需要为此问题编写Ajax代码 但是Django是否还有其他方法可以在不使用ajax的情况下获取下拉菜单中所选选项的值。
在purchase_settings.html中
<form method = "post" id="Purchase_setting" >
<div>Company Code:<br/>
<select name="cc_value" id="cc_value">
{% for cc_value in user_setting_list.9 %}
<option value="{{cc_value}}">{{cc_value}}</option>
{% endfor %}
</select>
</div>
<div >Purchase Organisation:<br/>
<select name="purch_org_value">
{% for purch_org in user_setting_list.10 %}
<option value="{{purch_org}}">{{purch_org}}</option>
{% endfor %}
</select>
</div>
<button class="button" type="submit">SAVE</i></button>
</form>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
$("#cc_value").change(function () {
var company_code = $(this).val();
$.ajax({
type: "GET",
data: {
'ccvalue': company_code
},
success: function (data) {
$("#purch_org_value").html(data);
}
});
});
</script>
在view.py
def user_settings(request):
....
// need to get cc_value in views on select without submiting form
cc_value1 = request.GET.get('ccvalue')
//on select of dropdown i m not getting value.I just tried giving cc_value = request.GET.get('cc_value')
print(cc_value1)//none
# user setting form
user_setting_list = user_setting.user_settings_form(client,login_username,db_username)
print(user_setting_list)
context = {
'user_setting_list':user_setting_list
}
return render(request, 'purchase_settings.html',context)