简体   繁体   中英

How to fix find and Field 'id' expected a number but got '' error

I am trying to add a like button to my posts in a Django Project. I have made a model for the likes and included a value for it. In the template the post_id should be reflected in the views but for some reason it is showing an error.

So, I have followed all the steps correctly but I am getting error:

ValueError at /blogs/like
Field 'id' expected a number but got ''.

Here is the models.py

class Post(models.Model):
    liked = models.ManyToManyField(User, default=None, blank=True, related_name='liked')
    ......................................

    @property
    def num_likes(self):
        return self.liked.all().count()
LIKE_CHOICES = (
    ('Like', 'Like'),
    ('Unlike', 'Unlike')
)


class Like(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    post = models.ForeignKey(Post, on_delete=models.CASCADE)
    value = models.CharField(choices=LIKE_CHOICES, default='Like', max_length=10)
    date_liked = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return str(self.post)

Here is the views.py

def like_post(request):
    user=request.user
    if request.method=='POST':
        post_id=request.POST.get('post_id')
        post_obj= Post.objects.get(id=post_id)<--------------- Error highlighted in this line

        if user in post_obj.liked.all():
            post_obj.liked.remove(user)
        else:
            post_obj.liked.add(user)

        like,created=Like.objects.get_or_created(user=user,post_id=post_id)

        if not created:
            if like.value=='Like':
                like.value='Unlike'
            else:
                like.value='Like'
        like.save()

    return redirect('blog:post_list')

Here is the template:

      <form action="{% url 'blog:like-post' %}" method="POST">
        {% csrf_token %}
        <input type="hidden" name="post_id" value='{{obj.id}}'>
        {% if user not in obj.liked.all %}
          <button class="ui button positive" type="submit">Like</button>
        {% else %}
          <button class="ui button negative" type="submit">Unlike</button>
        {% endif %}
        <strong>{{ obj.like.all.count }} Likes</strong>
      </form>

So the tutorial I was following have been using functions instead of Class based views which is the reason for causing me the error I assume, here is what I am trying to implement in my Detail View to get the same result:

    def post_view(request):
        qs=Post.objects.all()
        user=request.user
        context={
            'qs':qs,
            'user':user
        }
        return render(request'blog/post_detail.html', context)

How do I translate to my detailed view?

Try to print the post_id in views, and see the result in terminal,

post_id=request.POST.get('post_id')
print(post_id)

if it print an empty value, the passed value in template is not rendered the object properly..

In your views your are passing the context qs as the context I think it is the object you are trying to get the id.. It is a list of objects so you can access the id by one by one in a for loop manner

context={'qs':qs,'user':user}

It is a queryset so in the template:

{% for post in qs %}
   {{ post.id }}
   ...
   ...
{% endfor %}
   

Like the id you can access the other parameter defined in the model.. I think this will make you clear

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM