Django + HTML:为什么即使我没有提交兴趣,模板中的if条件也会失败? [英] Django + HTML: Why does my if condition in my template fail even thought I havent submitted interest?

查看:60
本文介绍了Django + HTML:为什么即使我没有提交兴趣,模板中的if条件也会失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django + HTML:为什么即使我没有提交兴趣,模板中的if条件也会失败?

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

从我的views.py中可以看到,我已经表明如果您已经提交了兴趣,您将收到一条消息,表明您已经提交了兴趣,否则将显示提交兴趣"按钮.为什么它不起作用?

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)

模板

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

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)

推荐答案

问题可能出在视图中您的if条件

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

在外部if条件中,您没有为 context ['submittedinterest'] 设置任何值,这意味着该值在上下文中将不存在.因此,在模板中,当您执行 {%if Submittedinterest%} 时,如果博客帖子属于您,它将始终评估为false.要解决此问题,您需要在外部else条件中执行 context ['submittedinterest'] = False .关于错误究竟在何处发生的问题,目前尚不清楚,我无法发表评论,因此我澄清了我认为错误可能是什么.如果您能澄清更多!

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!

这篇关于Django + HTML:为什么即使我没有提交兴趣,模板中的if条件也会失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆