【问题标题】:the username and request.user name has the same value but django python doesnt' recognise it as the same用户名和 request.user 名称具有相同的值,但 django python 不将其识别为相同
【发布时间】:2021-09-03 19:03:56
【问题描述】:

为什么我得到的不是同一个用户

这是我的观点.py

def upost(request, username):
if request.method == 'GET':
    vng_u = request.user
    us = username
    vd_u  = get_object_or_404(User, username=username)
    p_o_u = Name.objects.filter(user=vd_u).order_by('id').reverse()
    if request.user.is_anonymous:
        return redirect('login')
    else:
        if (vng_u == us):
            s = "same"
        else:
            s = "not"

        pgntr = Paginator(p_o_u,1)
        pn = request.GET.get('page')
        pgobj = pgntr.get_page(pn)
        return render(request, "network/users.html", {
            "xp": p_o_u,
            "pc": pgobj,
            "postuser": us,
            "uv":   vng_u,
            "s":s
        })

这是我的 html 中的内容

 {% if uv == postuser %}

相同 {%别的%} 不一样 {% 万一 %}

{{s}}

<div id="post-users"><br>
    Viewer:    <strong id="v1">{{uv}}</strong><br>
    Poster:    <strong id="v2">{{postuser}}</strong>
    {% for x in xp %}<br><hr>
        Content:    {{x.content}}<br>
                    {{x.timestamp}}
    {% endfor %}
</div>

这是出现在 html 网页上的内容

【问题讨论】:

  • 我建议您更详细地命名变量。 xpvnguv p_o_u 很难阅读。

标签: python html python-3.x django


【解决方案1】:

名称为fooUser 与字符串'foo' 不同。这意味着vng_u == us 将永远是False

您可以检查用户名是否匹配:

if vng_u.username == us:
    # …
    pass

或者你可以用给定的用户名检索用户,然后检查它是否等于vng_u

vd_u = get_object_or_404(User, username=username)
if vng_u == vd_u:
    …
    pass

【讨论】:

  • 非常感谢,我已经解决了一个星期,我真的厌倦了解决这个问题,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-09
  • 2017-03-03
  • 1970-01-01
相关资源
最近更新 更多