'Getting Error with django - Field 'id' expected a number but got '{}'
I'm coding a book store and on the single product page above the title of the book I have a link that directs the customer to the author profile page. The link is showing fine but when I click on it i get this error:
ValueError at /book_details/{}
Field 'id' expected a number but got '{}'.
Request Method: GET
Request URL: http://127.0.0.1:8001/book_details/%7B%7D
Django Version: 4.0.1
Exception Type: ValueError
Exception Value:
Field 'id' expected a number but got '{}'.
Exception Location: /Users/ariethan/Documents/django_apps/ibdb/virt/lib/python3.8/site-packages/django/db/models/fields/__init__.py, line 1824, in get_prep_value
Python Executable: /Users/ariethan/Documents/django_apps/ibdb/virt/bin/python
Python Version: 3.8.9
Python Path:
['/Users/ariethan/Documents/django_apps/ibdb/ibdb',
'/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python38.zip',
'/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8',
'/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/lib-dynload',
'/Users/ariethan/Documents/django_apps/ibdb/virt/lib/python3.8/site-packages']
Server time: Thu, 13 Jan 2022 09:43:37 +0000
/Users/ariethan/Documents/django_apps/ibdb/ibdb/ibdb_app/views.py, line 53, in book_details
book_details = Book.objects.get(pk=book_id)
urls.py
path('author_details/<ath_id>',views.author_details,name='author_details'),
view.py
def author_details(request,ath_id):
author_details = BookAuthor.objects.get(pk=ath_id)
return render(request,'author_details.html',{
'author_details':author_details,
})
html
<a href="{% url 'author_details' ath.id %}">{{book_details.book_author}}</a>
I tried some of the solutions but none seemed to work.
Solution 1:[1]
hi because you pass a {} to your URL /book_details/{} and in your view try to get a integer id like : /book_details/12
if read carefully errors you can find mistakes
ValueError at /book_details/{}
Field 'id' expected a number but got '{}'.
Request Method: GET
Request URL: http://127.0.0.1:8001/book_details/%7B%7D
Solution 2:[2]
I think this might be helpful:
def author_details(request,ath_id):
author_details = BookAuthor.objects.get(ath_id=author_details)
return render(request,'author_details.html',{
'author_details':author_details,
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Nova |
Solution 2 | RiveN |