Django + HTML: Why does my if condition in my template fail even thought I havent submitted interest?

心不动则不痛 提交于 2021-01-29 06:11:59

问题


Django + HTML: Why does my if condition in my template fail even thought I havent submitted interest?

As you can see from my views.py i have already indicated that if you have submitted interest, you will get the message that you have submitted the interest, otherwise the button 'submit interest' will be shown. Why does it not work then?

views.py

def detail_blog_view(request, slug):

    context = {}
#need to import a package get_object_or_404. return object or throw 404
    blog_post = get_object_or_404(BlogPost, slug=slug)
    total_likes = blog_post.total_likes()
    liked = False
    if blog_post.likes.filter(id=request.user.id).exists():
        liked = True
    context['liked'] = liked
    context['blog_post'] = blog_post
    context['total_likes'] = total_likes
    account = Account.objects.all()
    context['account'] = account
    #when post does not belong to me
    if blog_post.author != request.user:
        submittedinterest = False
        if blog_post.author.interestsender.filter(id=request.user.id).exists():
            submittedinterest = True
        context['submittedinterest'] = submittedinterest
    #when the post belongs to me
    else:
        pass 
    
    return render(request, 'HomeFeed/detail_blog.html', context)

template

{% if submittedinterest %}
     <a href="{% url 'HomeFeed:submitinterest' blog_post.slug %}"><button type="button" class="btn btn-warning">Collaborate!</button></a>
      {% else %}
      <div class="alert alert-success" role="alert">
      <p class="text-center">You have submitted your interest.</p>
    </div>
      {% endif %}

models.py

class BlogPost(models.Model):
 chief_title                    = models.CharField(max_length=50, null=False, blank=False)
 body                   = models.TextField(max_length=5000, null=False, blank=False)
 likes = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='blog_posts', blank=True)
 author                     = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
 slug                   = models.SlugField(blank=True, unique=True)

class Interest(models.Model):
   interestsender = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='interestsender', on_delete=models.CASCADE)
   interestreceiver = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='interestreceiver', on_delete=models.CASCADE)
   blog_post = models.ForeignKey(BlogPost, on_delete=models.CASCADE)


class Account(AbstractBaseUser):
 email                  = models.EmailField(verbose_name="email", max_length=60, unique=True)
 username               = models.CharField(max_length=30, unique=True)


回答1:


The issue might be with your if condition in the view

if blog_post.author != request.user:
    submittedinterest = False
    if blog_post.author.interestsender.filter(id=request.user.id).exists():
        submittedinterest = True
    context['submittedinterest'] = submittedinterest
#when the post belongs to me
else:
    pass  //this statement

In the outer if condition, you are not setting any value to the context['submittedinterest'] which means it will not exist in the context. Hence in the template when you do {% if submittedinterest %} it will always evaluate to false if the blog post belongs to you. To fix this you need to do context['submittedinterest'] = False in the outer else condition. The question isn't very clear regarding where exactly the error is occuring, and I cannot comment so I clarified what I think the error could be. If you can clarify more!



来源:https://stackoverflow.com/questions/65581475/django-html-why-does-my-if-condition-in-my-template-fail-even-thought-i-haven

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!