1

I'm new to django and I'm trying to create a like button for my blog posts. In my HomeFeed app

However, I received this error when i click on the like button that i have created:

ValueError invalid literal for int() with base 10: 'uien-jdhn-fds'

I am given a pointer that the problem comes from this statement:

post = get_object_or_404(BlogPost, id=request.POST.get('blog_post_slug'))

Is it because they are expecting all integers in the url but it is getting character values? I am unsure of how to change it.

Views.py

def LikeView(request, slug):
    context = {}
    post = get_object_or_404(BlogPost, id=request.POST.get('blog_post_slug'))
    if post.likes.filter(id=request.user.id).exists():
        post.likes.remove(request.user)
    else:
        post.likes.add(request.user)

    return HttpResponseRedirect(reverse('HomeFeed:detail',args=[str(BlogPost.slug)]))

def detail_blog_view(request, slug):

    context = {}
#need to import a package get_object_or_404. return object or throw 404
    blog_post = get_object_or_404(BlogPost, slug=slug)
    total_likes = blog_post.total_likes()
    context['blog_post'] = blog_post
    context['total_likes'] = total_likes
    return render(request, 'HomeFeed/detail_blog.html', context)

models.py

class BlogPost(models.Model):
 chief_title                    = models.CharField(max_length=50, null=False, blank=False)
 body                   = models.TextField(max_length=5000, null=False, blank=False)
 likes = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='blog_posts', blank=True)
 slug                   = models.SlugField(blank=True, unique=True)

 def __str__(self):
  return self.chief_title

 def total_likes(self):
   return self.likes.count()
   

urls.py

from .views import(
detail_blog_view,
LikeView,
)

urlpatterns = [
    path('<slug>/detail/', detail_blog_view, name= "detail"),
    path('<slug>/edit/', edit_blog_view, name= "edit"),
    path('<slug>/like/', LikeView, name='like_post'),
]

detail_blog.html

     <form action="{% url 'HomeFeed:like_post' blog_post.slug %}" method="POST" >{% csrf_token %} <button type="submit" name="blog_post_slug" value="{{blog_post.slug}}" class='btn btn-primary btn-sm'>Like</button> {{ total_likes }} Likes</form>

the following is the traceback:

Internal Server Error: /HomeFeed/uien-jdhn-fds/like/
Traceback (most recent call last):
  File "lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "views.py", line 195, in LikeView
    post = get_object_or_404(BlogPost, id=request.POST.get('blog_post_slug'))
  File "/lib/python3.8/site-packages/django/shortcuts.py", line 93, in get_object_or_404
    return queryset.get(*args, **kwargs)
  File "lib/python3.8/site-packages/django/db/models/query.py", line 399, in get
    clone = self.filter(*args, **kwargs)
  File "/lib/python3.8/site-packages/django/db/models/query.py", line 892, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "lib/python3.8/site-packages/django/db/models/query.py", line 910, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1290, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1315, in _add_q
    child_clause, needed_inner = self.build_filter(
  File "/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1251, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "lib/python3.8/site-packages/django/db/models/sql/query.py", line 1116, in build_lookup
    lookup = lookup_class(lhs, rhs)
  File "lib/python3.8/site-packages/django/db/models/lookups.py", line 20, in __init__
    self.rhs = self.get_prep_lookup()
  File "/lib/python3.8/site-packages/django/db/models/lookups.py", line 70, in get_prep_lookup
    return self.lhs.output_field.get_prep_value(self.rhs)
  File "lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 972, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'uien-jdhn-fds'
James Z
  • 12,209
  • 10
  • 24
  • 44

1 Answers1

0

You submitted the slug, not the primary key, so:

def LikeView(request, slug):
    context = {}
    post = get_object_or_404(BlogPost, slug=request.POST.get('blog_post_slug'))
    # …

It is however not necessary to submit this through the button, you already do this through the path, so you can make use of:

def LikeView(request, slug):
    context = {}
    post = get_object_or_404(BlogPost, slug=slug)
    # …

Furthermore you should also use post.slug, or simply slug when you reverse, so:

from django.shortcuts import redirect

def LikeView(request, slug):
    # …
    return redirect('HomeFeed:detail', slug=slug)
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • 1
    Hello thank you for the reply! i tried both your methods before but i got :No BlogPost matches the given query. –  Dec 30 '20 at 15:07
  • not sure why when i do that they cannot find my blog post –  Dec 30 '20 at 15:08
  • @JackalLee: well that means your template uses a slug for which no `BlogPost` exists. – Willem Van Onsem Dec 30 '20 at 15:09
  • @JackalLee: exactly where does the `blog_post.slug` in your template comes from? – Willem Van Onsem Dec 30 '20 at 15:09
  • it came from my detail_blog_view where i gave the context –  Dec 30 '20 at 15:11
  • @JackalLee: please [edit](https://stackoverflow.com/posts/65509019/edit) the question and include the `detail_blog_view`. – Willem Van Onsem Dec 30 '20 at 15:12
  • if that is wrong, do you have any idea how to reference that blog post, because for my edit blog and delete blog urls which is also in the same details_blog.html page, i also use blog_post.slug to reference it –  Dec 30 '20 at 15:12
  • detail_blog_view is in the views.py code. Kindly refer to it :) –  Dec 30 '20 at 15:13
  • @JackalLee: and there is a `BlogPost` with `uien-jdhn-fds` as slug? Exactly what page where you visiting when you submit the form? – Willem Van Onsem Dec 30 '20 at 15:16
  • uien-jdhn-fds is the slug for a blog post. I was visiting the detail blog page and there was a like button in the page –  Dec 30 '20 at 15:17
  • @JackalLee: what was the path of the detail page... – Willem Van Onsem Dec 30 '20 at 15:18
  • http://127.0.0.1:8000/HomeFeed/uien-jdhn-fds/detail/ –  Dec 30 '20 at 15:19
  • and when i press the like button i go to this url: http://127.0.0.1:8000/HomeFeed/uien-jdhn-fds/like/ –  Dec 30 '20 at 15:21
  • @JackalLee: ah, but likely this is when you reverse, since that contains an error as well... – Willem Van Onsem Dec 30 '20 at 15:22
  • perhaps would you require my code for my edit_blog_view as well as how i do the edit button and query the post for the edit as well? –  Dec 30 '20 at 15:24
  • ohhhh..any idea how to solve this reverse problem hahaa –  Dec 30 '20 at 15:24
  • @JackalLee: no, you simply need to `return redirect('HomeFeed:detail', slug=slug)` as HTTP result. – Willem Van Onsem Dec 30 '20 at 15:24
  • hi @WillemVanOnsem, think you can help with this question haha, nobody could answer it :( https://stackoverflow.com/questions/65581475/django-html-why-does-my-if-condition-in-my-template-fail-even-thought-i-haven –  Jan 05 '21 at 15:45