Django error while creating the blog web application
urls.py
path("blogpost/<int:id>", views.blogpost, name="blogpost)
views.py
def blogpost(request, id):
post = Blog1.objects.filter(post_id = id)[0]
return render(request, 'blogpost.html', {'post':post})
I'm having an error that the Blog1 object is iterable.
I tried everything, and my model has been created as well for blog1, and the primary key has issues under post_id, but I don't know why I am having this error about the object is iterabl. Any idea??
i think you want this...?
def blogpost(request, id):
post = Blog1.objects.get(id=id)
return render(request, 'blogpost.html', {'post':post})
It is useful to use the get_object_or_404 function when trying to import a single object.
from django.shortcuts import get_object_or_404
from .models import Blog1
def blogpost(request, id):
post = get_object_or_404(Blog1, id=id)
return render(request, 'blogpost.html', {'post':post})
When you are trying to declare single object always use get_object_or_404 in django.
from django.shortcuts import get_object_or_404 from .models import Blog1
def blogpost(request, id): post = get_object_or_404(Blog1, id=id) return render(request, 'blogpost.html', {'post':post})
It worked for me and useful for others as well.