1👍
You should retrieve a single item with .get(…)
[Django-doc], or even better, with get_object_or_404(…)
[Django-doc]:
from django.shortcuts import get_object_or_404
def blog(request, pk):
blog = get_object_or_404(Blogmodel, pk=pk)
comments = blog.comments.filter(active=True)
context = {'blog': blog, 'comments':comments}
return render(request, 'blogapp/blogpk.html', context)
Note: Models normally have no
Model
suffix. Therefore it might be better to renametoBlogModel
Blog
.
Source:stackexchange.com