Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django template comparing strings returning false?

I have in my template a button that should only be visible if

1 The user is signed in and;
2 The user is the one who posted the listing

but doesnt show up even if all the above conditions have been met,
so i have this in my template:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{% if user.username == "{{ users }}" %}
    <form action="listing" method="GET">
        <input class="btn btn-primary" type="submit" value="Close Listing" name="close">
    </form>
{% endif %}

where "{{users}}" is the username of whoever posted the listing, and thus if its a match, this user gets the button

views.py

listing = Listing.objects.all().filter(title=title).first()
user = listing.user

passing it to the template:

return render(request, "auctions/listing.html", {
     "users": user
})

Cant find what i am doing wrong

>Solution :

Change your logic;

First:

listing = Listing.objects.all().filter(title=title).first()

# Change variable name, you should name your variables with their purpose
author = listing.user

return render(request, "auctions/listing.html", {
     "author": author
})

Second:

# No need to use {{ }} in condition because `Jinja` does it for you.
{% if request.user == author %} # {% if request.user.username == author.username %}
    <form action="listing" method="GET">
        <input class="btn btn-primary" type="submit" value="Close Listing" name="close">
    </form>
{% endif %}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading