首页 > 解决方案 > 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

标签: pythondjangopostgresqlweb

解决方案


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.


推荐阅读