首页 > 解决方案 > 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 the error

标签: djangodjango-templatesdjango-urls

解决方案


尝试这个

url: "{% url 'getPatientDetails' id=patient.id %}", 

您需要传递 id

https://docs.djangoproject.com/en/3.1/ref/urls/#path


推荐阅读