简体   繁体   中英

Django If statement not working in template

i am adding this code to my template while using the django authetication system (USER) and the entry is a model with the entry_author field but its still didnt show me the resulr

{% if user.username == entry.entry_author %}
  <p class="btn btn-primary btn-sm">Delete Post &rarr;</p>
{% endif %}

Assuming that you have model like below:

class Entry(models.Model):
     entry_author = models.ForeignKey(User...)

what you should do is:

{% if request.user == entry.entry_author %}

Also you might want to add another layer of if statement to verify the request.user is authenticated.

{% if request.user.is_authenticated %}
     {% if request.user == entry.entry_author %}

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