python - get_object_or_404 can't find object with primary key
问题描述
My function in views.py is:
def detail(request,pk):
singlepost=get_object_or_404(iblog,pk=pk)
return render(request,'blog/blogpost.html',{'singlepost':singlepost})
My databse has several items but it return 404 error why ? Also i use
path('<int:pk>/',views.detail,name='detail')
for mapping url and calling detail function in views.py i test other parts of my project without get_object_or_404 function and every thing go right My problem is in primary key How can i resolve this problem? Thanks
解决方案
It looks like the primary key that you're trying to access doesn't exist in the model. This can happen if you delete some objects.
Default primary key attribute added by Django to the model is id
. Check if you're accessing a valid object and try again.
推荐阅读
- java - 如何将 Multipart 参数中的对象列表绑定到 objectMapper?
- javafx - 如何打印 TextArea 的内容?
- angular - Angular Schematics - 如何动态创建文件?
- opencv - 使用 OpenCV 将图像保存到我选择的另一个文件夹(带有“~”)
- php - 有没有办法从代码中导入 json 模板文件并在带有 Elementor 的页面上使用它们?
- java - 如何编写java关闭连接?
- xsd - 使用来自另一个 xsd 的选择性元素替换来解组 xsd
- c++ - 使用 QFileDialog 程序几秒钟后崩溃
- c# - 使用 JSON.NET 生成截断的 JSON
- utf-8 - Unicode 和 ASCII 运算符之间的区别