程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了/like/ 字段 'id' 处的 ValueError 需要一个数字,但得到了 ''大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决/like/ 字段 'id' 处的 ValueError 需要一个数字,但得到了 ''?

开发过程中遇到/like/ 字段 'id' 处的 ValueError 需要一个数字,但得到了 ''的问题如何解决?下面主要结合日常开发的经验,给出你关于/like/ 字段 'id' 处的 ValueError 需要一个数字,但得到了 ''的解决方法建议,希望对你解决/like/ 字段 'id' 处的 ValueError 需要一个数字,但得到了 ''有所启发或帮助;

我有一个赞按钮,以前运行良好,现在停止运行

@login_required
def like(request):
    post_ID = request.GET.get("likEID","")
    user = request.user
    post = PostForNewsFeed.objects.get(pk=post_ID)
    liked= false
    like = like.objects.filter(user=user,post=post)
    if like:
        like.delete()
    else:
        liked = True
        like.objects.create(user=user,post=post)
    resp = {
        'liked':liked
    }
    response = Json.dumps(resp)
    return httpResponse(response,content_type = "application/Json")


 <button class="btn btn-info" ID="{{ post.ID }}">
            {% if post in liked_post %}
            <a
              href="{% url 'post-like' %}"
              style="color: white"
              ID="likebtn{{ post.ID }}"
            >
              Unlike</a
            >
            | {{post.likes.count}} {% else %}
            <a
              href="{% url 'post-like' %}"
              style="color: white"
              ID="likebtn{{ post.ID }}"
            >
              like</a
            >
            | {{post.likes.count}} {% endif %}
          </button>

urls.py:

path('like/',vIEws.like,name='post-like'),`enter code here`

当我点击喜欢按钮时出现错误:

ValueError at /like/
FIEld 'ID' expected a number but got ''.


TraceBACk (most recent call last):
  file "C:\Python\lib\site-packages\django\core\handlers\exception.py",line 47,in inner
    response = get_response(request)
  file "C:\Python\lib\site-packages\django\core\handlers\base.py",line 181,in _get_response
    response = wrapped_callBACk(request,*callBACk_args,**callBACk_kwargs)
  file "C:\Python\lib\site-packages\django\contrib\auth\decorators.py",line 21,in _wrapped_vIEw
    return vIEw_func(request,*args,**kwargs)
  file "D:\ProgrAMMing\NewProjecttoReviseAllTechnologIEs\Django Social Networking Website\djangoNewEnv - Blog Project Before 

    Buddies\django\mysite\Feed\vIEws.py",line 154,in like
        post = PostForNewsFeed.objects.get(pk=post_ID)
      file "C:\Python\lib\site-packages\django\db\models\manager.py",line 85,in manager_method
        return getattr(self.get_queryset(),Name)(*args,**kwargs)
      file "C:\Python\lib\site-packages\django\db\models\query.py",line 424,in get
        clone = self._chain() if self.query.combinator else self.filter(*args,line 941,in filter
        return self._filter_or_exclude(false,args,kwargs)
      file "C:\Python\lib\site-packages\django\db\models\query.py",line 961,in _filter_or_exclude
        clone._filter_or_exclude_inplace(negate,line 968,in _filter_or_exclude_inplace
        self._query.add_q(Q(*args,**kwargs))
      file "C:\Python\lib\site-packages\django\db\models\sql\query.py",line 1391,in add_q
        clause,_ = self._add_q(q_object,self.used_aliases)
      file "C:\Python\lib\site-packages\django\db\models\sql\query.py",line 1413,in _add_q
        split_subq=split_subq,check_filterable=check_filterable,file "C:\Python\lib\site-packages\django\db\models\sql\query.py",line 1345,in build_filter
        condition = self.build_lookup(lookups,col,value)
      file "C:\Python\lib\site-packages\django\db\models\sql\query.py",line 1191,in build_lookup
        lookup = lookup_class(lhs,rhs)
      file "C:\Python\lib\site-packages\django\db\models\lookups.py",line 25,in __init__
        self.rhs = self.get_prep_lookup()
      file "C:\Python\lib\site-packages\django\db\models\lookups.py",line 77,in get_prep_lookup
        return self.lhs.output_fIEld.get_prep_value(self.rhs)
      file "C:\Python\lib\site-packages\django\db\models\fIElds\__init__.py",line 1827,in get_prep_value
        ) from e
    ValueError: FIEld 'ID' expected a number but got ''.
    [13/Jun/2021 13:01:43] "GET /like/ http/1.1" 500 181410

解决方法

url 路径应该包含一个 id 参数:

path('like/<int:id>/',views.like,name='post-like')

在您看来,我们可以与:

@login_required
def like(request,id):
    user = request.user
    post = PostForNewsFeed.objects.get(pk=id)
    liked= false
    like = Like.objects.filter(user=user,post=post)
    if like:
        like.delete()
    else:
        liked = True
        Like.objects.create(user=user,post=post)
    resp = {
        'liked':liked
    }
    response = json.dumps(resp)
    return httpResponse(response,content_type = "application/json")

因为您在视图函数中使用了 id 参数。

您需要在 {% url … %} template tag [Django-doc] 中传递 id:

<!--                           ↓ add the id of the post -->
<a href="{% url 'post-like' post.id %}" style="color: white" id="likebtn{{ post.id }}">

模板中指向 {% url … %} 视图的其他 post-like 也应该发生同样的情况。

通常情况下,有副作用的操作应该通过 POST、PUT、PATCH 或 deletE 请求完成,而不是 GET 请求。因此,制作一个“迷你”表单来发出 POST 请求更有意义。

大佬总结

以上是大佬教程为你收集整理的/like/ 字段 'id' 处的 ValueError 需要一个数字,但得到了 ''全部内容,希望文章能够帮你解决/like/ 字段 'id' 处的 ValueError 需要一个数字,但得到了 ''所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。