django - Ajax request cannot access the URL in Django. I have a problem with an integer( the id)
问题描述
I'm working on an app for patients using Django. I tried to get all the details about a patient and display them on a new panel. When the user presses a button I use the patient's id. When I try to put this id in ajax's URL I receive an error "NoReverseMatch". I don't know how to solve it. Any help is welcome. Sorry for the indentation, but this is Atom :))
I have two ajax requests. The first one is functional( here I display all patients' names in a table, for every patient I attach a button named "Details". When the user clicks the "Details" button for a patient I want to display all data about this person on a new panel using a href to the second ajax). I have a "Delete" button too, but it's functional, the "Details" is the problem here.
views.py
def getPatientDetails(request, id):
currentPatient = Patient.objects.get(id=id)
return JsonResponse({"patient":currentPatient})
urls.py for this function
path('getPatientDetails/<int:id>', getPatientDetails, name='getPatientDetails'),
Here is the html template
This is the first ajax, which is functional
<script>
$(document).ready(function(){
$.ajax({
type:'GET',
url: "{% url 'getPatients' %}",
success: function(response){
$("#displayPatients").empty();
for(var key in response.patients) {
var temp = "<tr><td>"+response.patients[key].firstName+"</td><td>"+response.patients[key].lastName+"</td><td>"+response.patients[key].phone+"</td><td><a href='getPatientDetails/"+response.patients[key].id+"'><button class='detailsButton outline-text'>Details</button></a><a href='deletePatient/"+response.patients[key].id+"'><button class='deleteButton outline-text'>Delete</button></a></td></tr>";
$("#displayPatients").append(temp);
}
},
error: function(response){
alert("NO DATA FOUND!!!");
}
});
});
</script>
This is the second ajax request which has problems with the url
<script>
$(document).ready(function(){
$.ajax({
type:'GET',
url: "{% url 'getPatientDetails/<int:id>' %}", // here is the problem. This type of request doesn't accept the <int:id> (Django logic)
success: function(response){
$("#patientDetails").empty();
var temp = response.patient;
console.log(temp.firstName); // for debug in web console
// Here I will display all data about this patient on a new panel
},
error: function(response){
alert("NO DATA FOUND!!!");
}
});
});
</script>
This is the Exception Value: 'getPatientDetails/<int' is not a registered namespace
解决方案
尝试这个
url: "{% url 'getPatientDetails' id=patient.id %}",
您需要传递 id
推荐阅读
- javascript - 使用键动态构建最小值和最大值
- python - Python Dash:如何使用自定义 CSS 设置 dash 组件的样式?
- powershell-core - PowerShell Core `Select-Xml` 不返回任何内容
- python - 在分隔符处加入 python 列表
- javascript - 强制 RTL 的 NativeBase 选项卡问题 - 修复
- javascript - 如何在谷歌应用程序制造商中修复文本框标签的位置?
- javascript - Node + websocket 向客户端发送消息
- sql - 连接到 SQL Server 时出错。服务器未找到或无法访问
- php - 从目录句柄读取条目的问题
- python-3.x - 在 python 中遍历文件夹时遇到一些问题