[英]Populate html element from selected dropdown value in Django
I am very new to django and AJAX and am trying to populate a HTML element (span for the time being) with data from a dropdown menu. 我对django和AJAX并不陌生,并且正在尝试使用下拉菜单中的数据填充HTML元素(暂时为跨度)。
My current approach is to get the unique ID for the row through the id tag in the <option>
element and post it with AJAX to views.py where that ID is then used as a filter in Django to GET the corresponding row. 我当前的方法是通过<option>
元素中的id标记获取该行的唯一ID,并将其与AJAX一起发布到views.py,然后将该ID用作Django中的过滤器以获取相应的行。 However, for some reason, whenever I try to filter the query nothing is returned. 但是,由于某种原因,每当我尝试过滤查询时,都不会返回任何内容。 Whenever I remove the filter, all of the values are parsed, making me think that I'm getting the filter wrong somehow. 每当我删除过滤器时,所有值都会被解析,让我觉得我以某种方式弄错了过滤器。 I've tried many of the different filter types, but I just can't seem to get it right. 我尝试了许多不同的过滤器类型,但似乎无法正确解决。
The HTML is: HTML是:
<select id="drugSet">
{% for dose in dose_set %}
<option id="{{ dose.pubmed_id }}">{{ dose.drug_name }}</option>
{% endfor %}
</select>
<span id="drugName"></span>
<a href="javascript:NeedDrugInformation()">Retrieve data</a>
The Javascript and AJAX are: Javascript和AJAX是:
function NeedDrugInformation() {
var elementID = document.getElementById("drugSet");
var drugID = String(elementID.options[elementID.selectedIndex].id);
$.ajax({
type: "POST",
url: "drugsanddoses/",
dataType: "json",
async: true,
data: { csrfmiddlewaretoken: '{{ csrf_token }}', drugID: drugID },
success : function(json) {
},
error : function(xhr,errmsg,err) {
}
});
$.ajax({
type: "GET",
url: "drugsanddoses",
dataType: "json",
async: true,
data: { csrfmiddlewaretoken: '{{ csrf_token }}' },
success: function (json) {
$('#drugName').html(json.drugInfo);
// $('.ajaxProgress').hide();
}
})
}
Views.py: Views.py:
def drugsanddoses(request):
drugID = request.POST.get('drugID')
drugInfo = RiskCalculator.objects.filter(pubmed_id='drugID').values('drug_name', 'l_dose', 'h_dose', 'risk', 'pubmed_id', 'updated')
response_data = {}
try:
response_data['drugInfo'] = str(drugInfo)
except:
response_data['result'] = 'No details found'
response_data['message'] = 'There is currently no information in the database for this drug.'
return HttpResponse(json.dumps(response_data), content_type="application/json")
As I said, I'm very new to this. 如我所说,我对此很陌生。 Any pointers or ways I could be doing this better would be much appreciated. 我会做得更好的任何指针或方法将不胜感激。
First of all the POST
field contains form posted values, not JSON. 首先, POST
字段包含表单发布的值,而不是JSON。 Parse Json payload first: 首先解析Json有效负载:
import json
drugID = json.loads(request.body).get('drugID')
Secondly you're using the string 'drugID'
in your filter instead of the value in variable drugID
. 其次,您在过滤器中使用字符串'drugID'
,而不是变量drugID
中的值。 Try changing query line to: 尝试将查询行更改为:
drugInfo = RiskCalculator.objects.filter(pubmed_id=drugID).values('drug_name', 'l_dose', 'h_dose', 'risk', 'pubmed_id', 'updated')
You should also use a JsonResponse never catch all exceptions. 您还应该使用JsonResponse,从不捕获所有异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.