阅读:114
在启动django框架运行之后,我进行对http://127.0.0.1:8000/home/query/地址的访问,这个页面中使用了ajax请求,本来是通过ajax请求路口到后端,然后后端从数据库中查找数据再返回到前端。
我在html文件中的ajax请求是这样写的:
<script type="text/javascript">
$(function () {
//页面加载完成之后自动执行
bindBtn2Event();
})
{# 数据统计模块 2022.5.1 #}
/**
* 初始化柱状图、折线图、雷达图
* */
function bindBtn2Event() {
$("#btn2").click(function () {
$.ajax({
url: '/home/ajax/',
type: 'post',
data: $("#form_show").serialize(),
success: function (res) {
// 将后台返回的数据更新到option中
if (res.status) {
console.log("成功!")
}
}
})
})
}
</script>
正常情况下,应该会在console中打印“成功”。但是出现了一个错误:并没有将“成功”返回到前端。
我右击“检查”打开浏览器源码,在console中发现html文件中出现了红线框里的ajax请求失败,这是为什么呢?
而且在PyCharm里也报错了:
Internal Server Error: /home/ajax/
Traceback (most recent call last):
File "D:\env\first\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "D:\env\first\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "D:\env\first\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\env\first\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "D:\太原理工2018\代码\baizhi\tyut2018\PythonWeb\zx\DjangoProject01\home\views.py", line 34, in home_ajax
road = Effect.objects.filter(id=data_list['road_id'][0])
File "D:\env\first\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "D:\env\first\lib\site-packages\django\db\models\query.py", line 904, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "D:\env\first\lib\site-packages\django\db\models\query.py", line 923, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "D:\env\first\lib\site-packages\django\db\models\sql\query.py", line 1351, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "D:\env\first\lib\site-packages\django\db\models\sql\query.py", line 1378, in _add_q
child_clause, needed_inner = self.build_filter(
File "D:\env\first\lib\site-packages\django\db\models\sql\query.py", line 1251, in build_filter
lookups, parts, reffed_expression = self.solve_lookup_type(arg)
File "D:\env\first\lib\site-packages\django\db\models\sql\query.py", line 1088, in solve_lookup_type
_, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
File "D:\env\first\lib\site-packages\django\db\models\sql\query.py", line 1483, in names_to_path
raise FieldError("Cannot resolve keyword '%s' into field. "
django.core.exceptions.FieldError: Cannot resolve keyword 'id' into field. Choices are: avg_delay, avg_passed_vehicle_num, backbone, backbone_id, configuration, flow_volume, greenrate, level, paused_vehicle_num, queue_length, road_id, road_name, service_level, signal_rationality
其中,最主要的错误是最后一句:FieldError:无法将关键字“id”解析为字段。
django.core.exceptions.FieldError: Cannot resolve keyword 'id' into field.
Choices are: avg_delay, avg_passed_vehicle_num, backbone, backbone_id, configuration, flow_volume, greenrate, level, paused_vehicle_num, queue_length, road_id, road_name, service_level, signal_rationality
FieldError:无法将关键字“id”解析为字段。
(1)FieldError:无法将关键字“id”解析为字段。肯定是我哪个地方是“id”,但是我的数据库和model.py文件里并没有“id”字段, 就会发生这样的错误。
因此,我发现了这句查找语句写错了,“id”写错了,
(2) “id”写错了,应该写成与数据库的表Effect项对应的字段“road_id”
因为我的数据库的表Effect项对应的字段是“road_id”,而不是“id”
(3)改好之后,再次重启django项目之后,发现这个报错问题消失了,好了,问题成功解决!!
而且PyCarm也没有报错了:
OK,问题解决。