如何更正此Django异步json请求?
我有一个包含以下输入的简单表单: 键入radio并选择parent 根据所选的类型,应该有不同的选择选项。因此,无论何时选择类型,它都应该发出json请求,以获取基于该类型的父级列表 到目前为止,我有: 模板页面中的json请求如何更正此Django异步json请求?,json,django,Json,Django,我有一个包含以下输入的简单表单: 键入radio并选择parent 根据所选的类型,应该有不同的选择选项。因此,无论何时选择类型,它都应该发出json请求,以获取基于该类型的父级列表 到目前为止,我有: 模板页面中的json请求 $.getJSON("{% url 'locations_by_type' %}", {type: type}, function(locations) { var locations_select = $("select[name='parent']");
$.getJSON("{% url 'locations_by_type' %}", {type: type}, function(locations) {
var locations_select = $("select[name='parent']");
$.each(locations, function (index, value) {
locations_select.append($("<option/>", {
value: key,
text: value
}));
});
});
views.py
def locations_by_type(request):
locations = Location.objects.get(type=request.GET["type"])
return json.dumps(locations)
def locations_by_type(request):
locations = Location.objects.filter(type=request.GET["type"]).values('id', 'name')
data = json.dumps(list(locations), cls=DjangoJSONEncoder)
return HttpResponse(data, content_type='application/json')
URL.py单独模式
url(r'^/locations_by_type', views.locations_by_type, name='locations_by_type'),
当我加载表单时,表单本身可以正常加载,但是我从javascript控制台的请求中得到一个错误:
GET http://locations_by_type/?type=state net::ERR_NAME_NOT_RESOLVED
你知道我做错了什么吗
更新1
我以前在url regex中的locations_by_类型前面有一个/。修复此问题后,javascript控制台报告了一个500内部服务器错误
manage.py runserver窗口中记录的Django错误为:
TypeError: <Location: SC> is not JSON serializable
location.html脚本
<script>
//Update Location Select box given a type
function updateLocations(type) {
$.getJSON("{% url 'locations_by_type' %}", {type: type}, function(locations) {
var locations_select = $("select[name='parent']");
$.each(locations, function (index, value) {
locations_select.append($("<option/>", {
value: index,
text: value
}));
});
});
}
//If there is a type selected at document load, make sure parent select
// has the appropriate values, otherwise hide it
$(document).ready(function() {
if ($("input[name='type']:checked").size() == 0) {
$("select[name='parent']").parent().parent().hide();
} else {
var type = $("input[name='type']:checked").val();
updateLocations(type);
}
});
//Whenever type is changed, make sure the parent select is shown and has
// the appropriate values
$("input[name='type']").change(function() {
$("select[name='parent']").parent().parent().show();
var type = $("input[name='type']:checked").val();
updateLocations(type);
})
</script>
最后,我得到的回报是:
<select id="id_parent" name="parent">
<option value="0">[object Object]</option>
<option value="1">[object Object]</option>
....
</select>
不过,我想:
<select id="id_parent" name="parent">
<option value="[location.id]">[location.name]</option>
<option value="[location.id]">[location.name]</option>
....
</select>
此URL.py是否包含在其他URL.py中?如果是这样,您是否也可以发布父URL.py?在我看来,你的位置按类型路由被反向解析为//locations\u by\u type/而不是/locations\u by\u type/。呃,是的,第一个/不应该在那里。我修改了URL,但在那一点上它仍然会收到500个内部服务器错误。我同意@lanzz的说法,并且在我看来,如果使用设置,你会得到一个NoReverseMatch。Debug=True。你的{%url%}标记有问题,比如你需要app:locations_by_type语法。从url正则表达式中删除前面的/之后,ERR_NAME_NOT_RESOLVED被替换为一个500错误,该错误来自TypeError:JSON不可序列化吗?你们知道解决这个问题的方法吗?
<select id="id_parent" name="parent">
<option value="[location.id]">[location.name]</option>
<option value="[location.id]">[location.name]</option>
....
</select>