get() method is not working in Django,
I have a model BlogPost, when I try to fetch data from that model using get() method it shows
Error : 'BlogPost' object is not iterable
def blog_post_detail(request, slug):
query = BlogPost.objects.get(slug=slug)
template_name = 'blog/post.html'
context = {'query': query}
return render(request, template_name, context)
But the same thing works using filter() method
def blog_post_detail(request, slug):
query = BlogPost.objects.filter(slug=slug)
template_name = 'blog/post.html'
context = {'query': query,}
return render(request, template_name, context)
Note: I have only one post in BlogPost
BlogPostobject in the detail view.getreturns a single instance,filterreturns a queryset which is an iterable over instances.