Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

matching query does not exist. in django

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading