I’m trying to create a like/unlike button but this error keeps showing after several attempts at tweaking this
ValueError at /update_like
Field ‘id’ expected a number but got ‘’.
VIEWS.PY
def update_like(request):
username = request.user.username
post_id = request.GET.get('post_id')
post = NewTweet.objects.get(id = post_id)
like_filter = LikesPost.objects.filter(post_id=post_id, username=username).first
if like_filter == None:
new_like = LikesPost.objects.create(post_id=post_id, username=username)
new_like.save()
post.save()
return HttpResponseRedirect(reverse("index"))
else:
like_filter.delete()
post.save()
return HttpResponseRedirect(reverse("index"))
INDEX.HTML
<div class="tweet-feed">
{% for post in all_post%}
<div class="tweet">
<img src="http://placeimg.com/140/140/people" alt="author profile picture" class="tweet-author-image"/>
<div class="tweet-feed-content">
<div class="tweet-header">
<a href="#" class="tweet-author-username">{{post.user}}</a>
<div class="tweet-author-handle">@ {{post.user}</div>
<div class="tweet-dot">.</div>
<div class="tweeted-time">{{post.created_at}}</div>
</div>
<div class="tweet-content">
<div class="tweet-created-content">{{post.caption}}</div>
</div>
<span id="likeCount"></span>
<div class="engagement-icons">
<ul class="engagement-icons-ul mt-1">
<li><i class="fa-regular fa-comment"></i></li>
<li><i class="fa-solid fa-retweet"></i></li>
<li><a href="/update_like?post_id={{post.id}}"><i id="LikeBtn" class="fa-regular fa-heart"></i></a>
</li>
<li><i class="fa-solid fa-arrow-up-from-bracket"></i></li>
</ul>
</div>
</div>
</div>
{% endfor %}
MODELS.PY
class User(AbstractUser):
pass
class NewTweet(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
caption = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f"{self.user} posted {self.caption}"
class LikesPost(models.Model):
post_id = models.CharField(max_length=100)
username = models.ForeignKey(User, on_delete=models.CASCADE, )
def __str__(self):
return self.username
URLS.PY
path(“update_like”, views.update_like, name=“update_like”),
TRACEBACK
Traceback (most recent call last):
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\fields\__init__.py", line 1822, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: ''
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\USER\Desktop\CS50\project4\network\views.py", line 131, in update_like
like_filter = LikesPost.objects.filter(post_id=post_id, username=username).first
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 974, in filter
return self._filter_or_exclude(False, args, kwargs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 992, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 999, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\sql\query.py", line 1375, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\sql\query.py", line 1396, in _add_q
child_clause, needed_inner = self.build_filter(
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\sql\query.py", line 1329, in build_filter
condition = self.build_lookup(lookups, col, value)
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\sql\query.py", line 1180, in build_lookup
lookup = lookup_class(lhs, rhs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\lookups.py", line 22, in __init__
self.rhs = self.get_prep_lookup()
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\fields\related_lookups.py", line 120, in get_prep_lookup
self.rhs = target_field.get_prep_value(self.rhs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\fields\__init__.py", line 1824, in get_prep_value
raise e.__class__(
ValueError: Field 'id' expected a number but got ''.
[08/Jan/2023 19:20:02] "GET /update_like?post_id=4 HTTP/1.1" 500 122943
From the research I’ve done, I suspect the issue is coming from Models - username = models.ForeignKey(User, on_delete=models.CASCADE) - but I don’t know how to rectify it