Ad

Matching Query Does Not Exist. In Django

- 1 answer

Hi This is my Project I needed define two get_absolute_url in my project get_absolute_url one

models.py

def get_absolute_url(self):
    return reverse("cv:resume_detial", args={self.id, self.slug})

urls.py

path('resume-detial/<int:resume_id>/<slug:resume_slug>', views.ResumeDetialView.as_view(), name="resume_detial"),

views.py

class ResumeDetialView(View):
    template_name = 'cv/resume-detial.html'
    
    def get(self, request, resume_id, resume_slug):
        resumes = ResumeDetial.objects.get(pk=resume_id, slug=resume_slug)
        return render(request, self.template_name, {'resumes':resumes})

This is a work But other get_absolute_url is does not work

models.py

def get_absolute_url(self):
    return reverse("cv:project_detial", args={self.id, self.project_slug})

urls.py

path('project-detial/<int:project_id>/<slug:projects_slug>', views.ProjectDetialView.as_view(), name="project_detial"),

views.py

class ProjectDetialView(View):
    template_name = 'cv/project-detial.html'
    
    def get(self, request, project_id, projects_slug):
        projects = ResumeDetial.objects.get(pk=project_id, slug=projects_slug)
        return render(request, self.template_name, {"projects":projects})

django messages

DoesNotExist at /project-detial/1/todo-django
ResumeDetial matching query does not exist.

I am a beginner and thank you for your help

Ad

Answer

You are doing ResumeDetial.objects.get(pk=project_id, slug=projects_slug) while the ResumeDetial with these criteria doesn't exist. You can use filter to search for ResumeDetial's with the given criteria, in case no results are found, it will return an empty list.

So, you can change this part of the code:

    def get(self, request, project_id, projects_slug):
        projects = ResumeDetial.objects.filter(pk=project_id, slug=projects_slug)
        return render(request, self.template_name, {"projects":projects})
Ad
source: stackoverflow.com
Ad