【发布时间】:2021-03-17 15:06:53
【问题描述】:
我想从下拉菜单中选择一个order_id,这将最终查询我的 MySQL 数据以获取所选出租车的起点纬度/经度和终点纬度/经度。我看过在线教程,但是当我检查我的检查元素的网络选项卡时,我的下拉选择给了我以下信息:
[17/Mar/2021 14:58:29] "POST / HTTP/1.1" 403 2294
我的数据最终都没有被查询。
我做错了什么? (另外,因为我使用的是 Django 1.6.11,所以我插入了 JSONResponse 的代码,而不是从 django.http 导入)
base.html
<body>
<div class="order">
<h1>View and calculate distance per order</h1>
<select name="dropdown_menu" id="dropdown_menu">
<option class="dropdown" type="dropdown" selected>-- Select Order ID -- </option>
{% for order in orders %}
<!-- for x in {context}-->
<option value="{{ order.order_id }}"> {{ order.order_id }} </option>
{% endfor %}
</select>
<p>The selected order id is {{dropdown_menu}}</p>
<!-- render distance calculation -->
<!-- render distance on folium -->
</div>
{% block javascript %}
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script>
$("#dropdown_menu").change(function () { // calling select id
const order_id_selected = $(this).val(); // get the selected subject ID from the HTML dropdown list
$.ajax({ // initialize an AJAX request
type: "POST",
url: '',
data: {
'order_id_selected': order_id_selected, // add the order id to the POST parameters
'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val(),
},
success: function (data) { // `data` is from `homepage` view function
let html_data = '<option value="">---------</option>';
data.forEach(function (data) {
html_data += `<option value="${data.id}">${data.title}</option>`
});
$("#dropdown_menu").html(html_data); // replace the contents of the topic input with the data that came from the server
}
});
});
</script>
{% endblock javascript %}
</body>
views.py
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, Http404
from django.contrib import messages
from django.utils import simplejson
from .models import Member, Driver, Order, Distance
class JsonResponse(HttpResponse):
"""
Code for JSONResponse - not in Django 1.6.11
"""
def __init__(self, content, mimetype='application/json', status=None, content_type=None):
super(JsonResponse, self).__init__(
content=simplejson.dumps(content),
mimetype=mimetype,
status=status,
content_type=content_type,
)
# def test(request):
# return render(request, 'base.html')
# Create your views here.
def homepage(request):
orders = Distance.objects.all().order_by("order")
if request.method == "POST": #
print("POST")
order_id_selected = request.POST.get("dropdown_menu", None)
if order_id_selected:
print(order_id_selected)
try:
start_lat = Distance.object.filter(id = 'start_lat')
start_long = Distance.object.filter(id = 'start_long')
end_lat = Distance.object.filter(id = 'end_lat')
end_long = Distance.object.filter(id = 'start_long')
except Exception:
data['error_message'] = 'error'
return JsonResponse(data)
return render(request, 'base.html', {
# 'members': members,
# 'drivers': drivers,
'orders': orders,
'dropdown_menu': order_id_selected,
}
)
else: # current view defaults to else
return render(request, 'base.html', {
'orders': orders,
})
【问题讨论】:
-
看起来您正在将 id 发送到名为“order_id_selected”的字段中的视图,而不是“dropdown_menu”。看看
request.POST.get("order_id_selected", None) -
if order_id_selected:之后的代码也很奇怪。首先你写Distance.object,而它应该是Distance.objects(注意s)。另外,为什么您要尝试-除非无缘无故? (事实上,这些过滤器的用途是什么?) -
-
@cookiestarninja 你没有使用表单。您在发出 ajax 请求时写了这行
'order_id_selected': order_id_selected,,这意味着 Ben 上面所说的内容是正确的。 -
@Abdul Aziz Barkat + Ben 感谢您的澄清!