Following is my code in URL.py, views.py and HTML page. However, it returns me the error: TypeError: cannot unpack non-iterable int object.
urlpatterns = [
path('', views.blogs_home, name='blogs'),
path('<int:id>', views.single_blog, name='detailed_view'),
]
I am trying to capture the id of posts blogs in the list view to get the blog object from the database with id query. Following is my view code.
def single_blog(request,id):
blog_single = Blogs.objects.get(id)
context = {'blog_single': blog_single}
template = 'blog_home.html'
return render(request, template, context)
However, as I mentioned, it returns the above error.
Could someone explain what I am doing wrong
You should specify the name of the parameter in .filter(…)
[Django-doc] or .get(…)
[Django-doc] calls:
def single_blog(request, id):
blog_single = Blogs.objects.get(id=id)
context = {'blog_single': blog_single}
template = 'blog_home.html'
return render(request, template, context)
I also propose to rename your variable to something else (so both in the urls.py
and views.py
), since id
is a builtin function, and now a local variable is "hiding" this builtin.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With