[英]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嗨,这是我的项目,我需要在我的项目 get_absolute_url 中定义两个 get_absolute_url
models.py模型.py
def get_absolute_url(self):
return reverse("cv:resume_detial", args={self.id, self.slug})
urls.py网址.py
path('resume-detial/<int:resume_id>/<slug:resume_slug>', views.ResumeDetialView.as_view(), name="resume_detial"),
views.py视图.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这是一项工作,但其他 get_absolute_url 是行不通的
models.py模型.py
def get_absolute_url(self):
return reverse("cv:project_detial", args={self.id, self.project_slug})
urls.py网址.py
path('project-detial/<int:project_id>/<slug:projects_slug>', views.ProjectDetialView.as_view(), name="project_detial"),
views.py视图.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 django 条消息
DoesNotExist at /project-detial/1/todo-django
ResumeDetial matching query does not exist.
I am a beginner and thank you for your help我是初学者,感谢您的帮助
You are doing ResumeDetial.objects.get(pk=project_id, slug=projects_slug)
while the ResumeDetial with these criteria doesn't exist.您正在执行
ResumeDetial.objects.get(pk=project_id, slug=projects_slug)
而符合这些条件的 ResumeDetial 不存在。 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.您可以使用过滤器根据给定的条件搜索 ResumeDetial,如果没有找到结果,它将返回一个空列表。
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})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.