john foreign PH
john foreign PH

Reputation: 23

the username and request.user name has the same value but django python doesnt' recognise it as the same

why am i getting not the same user

Here is what in my views.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
        })

here is what in my html

 {% if uv == postuser %}

same {%else%} not the same {% endif %}

{{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>

and here is what appears on the html web pageit reads not the same

Upvotes: 1

Views: 176

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477180

A User with the name foo is not the same as a string 'foo'. This thus means that vng_u == us will always be False.

You can check if the user names match with:

if vng_u.username == us:
    # …
    pass

or you can retrieve the user with the given username, and then check if it is equal to vng_u:

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

Upvotes: 2

Related Questions