Home > Mobile >  django can't reverse match url from template
django can't reverse match url from template

Time:09-22

I am having difficulty reverse matching a url, getting the error:

NoReverseMatch at /patient/46cb4bd5-ef39-4697-84ff-9aa2b6e85e6b/
Reverse for 'treatment_detail' with no arguments not found. 1 pattern(s) tried: ['patient/(?P<patient_id>[^/] )/$']

The url is:

/patient/46cb4bd5-ef39-4697-84ff-9aa2b6e85e6b/

(the string is the 'apatient_id' and changes each time the user submits the 'add' page)

urls.py is

app_name = "patient"
urlpatterns = [
    path(
        route='add/',
        view=views.PatientAddView.as_view(),
        name="patient_add"),
    path(
        route='<patient_id>/',
        view=views.TreatmentTemplateView.as_view(),
        name='treatment_detail'),
]

html

<form action="{% url 'patient:treatment_detail' %}" method="get">
<input type="submit" class="btn btn-primary" value="get_doc" name="get_doc">
</form>

views.py

class TreatmentTemplateView(TemplateView):
    template_name = "../templates/patient/treatment_detail.html"

    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        context["patient_id"] = self.kwargs["patient_id"]
        result = find_treatment(context["patient_id"])
        context = result[0]
        context["patient"] = result[1]

        return context

class PatientAddView(LoginRequiredMixin, TemplateView):
    model = Patient
    template_name = "../templates/patient/add.html"

    def get(self, *args, **kwargs):
        patient_form = PatientForm
        currentmed_formset = CurrentmedFormSet(queryset=CurrentMed.objects.none())
        pastmed_formset = PastmedFormSet(queryset=PastMed.objects.none())
        diagnosis_formset = DiagnosisFormSet(queryset=Diagnosis.objects.none())
        problem_formset = ProblemFormSet(queryset=Problem.objects.none())
        sideeffect_formset = SideeffectFormSet(queryset=SideEffect.objects.none())


        return self.render_to_response(
            {
                "diagnosis_formset": diagnosis_formset,
                "problem_formset": problem_formset,
                "sideeffect_formset": sideeffect_formset,
                "currentmed_formset": currentmed_formset,
                "pastmed_formset": pastmed_formset,
                "patient_form": patient_form,
                "med_formsethelper": MedFormSetHelper,
                "problem_formsethelper": ProblemFormSetHelper,
                "diagnosis_formsethelper": DiagnosisFormSetHelper,
                "sideeffect_formsethelper": SideEffectFormSetHelper,
            }
        )

    def post(self, *args, **kwargs):
        form = PatientForm(data=self.request.POST)
        currentmed_formset = CurrentmedFormSet(data=self.request.POST)
        pastmed_formset = PastmedFormSet(data=self.request.POST)
        diagnosis_formset = DiagnosisFormSet(data=self.request.POST)
        problem_formset = ProblemFormSet(data=self.request.POST)
        sideeffect_formset = SideeffectFormSet(data=self.request.POST)

        if form.is_valid():
            print("pt_valid")
            patient_instance = form.save()
            patient_instance.user = self.request.user
            patient_instance.save()

            if diagnosis_formset.is_valid():
                print("diag_valid")
                diag_name = diagnosis_formset.save(commit=False)
                for diag in diag_name:
                    diag.patient = patient_instance
                    diag.save()

            if problem_formset.is_valid():
                prob_name = problem_formset.save(commit=False)
                for prob in prob_name:
                    prob.patient = patient_instance
                    prob.save()

            if sideeffect_formset.is_valid():
                se_name = sideeffect_formset.save(commit=False)
                for se in se_name:
                    se.patient = patient_instance
                    se.save()

            if currentmed_formset.is_valid():
                med_name = currentmed_formset.save(commit=False)
                for med in med_name:
                    med.patient = patient_instance
                    med.save()

            if pastmed_formset.is_valid():
                med_name = pastmed_formset.save(commit=False)
                for med in med_name:
                    med.patient = patient_instance
                    med.save()

        return redirect(
            reverse(
                "patient:treatment_detail",
                kwargs={"patient_id": patient_instance.patient_id},
            )
        )


If I have 'patient:patient_add' instead of 'patient:treatment_detail' it works fine so the issue seems to be about the route="<patient_id>"/ In urls.py

CodePudding user response:

Your url route should include patient_id such that:

path(
    route='<uuid:patient_id>/',
    view=views.TreatmentTemplateView.as_view(),
    name='treatment_detail'),

I assume you're using uuid field in your model. You can take a look at the docs for more detail.

In your template, you should also pass uuid field to the template tag:

"{% url 'patient:treatment_detail' patient.patient_id %}"

  • Related