【问题标题】:a post with border and no border using for or if in django使用 for 或 if 在 django 中的有边界和无边界的帖子
【发布时间】:2020-05-07 11:38:52
【问题描述】:

models.py:

class post(models.Model):
    title = models.CharField(max_length=40)
    picture = models.ImageField(upload_to='somewhere/dir')
    active_post = models.BooleanField(default=True)
    commercial_post = models.BooleanField(default=False)
    normal_post = models.BooleanField(default=True)
    post_with_colored_border = models.BooleanField(default=False)
    post_title_bold = models.BooleanField(default=False)
    post_with_border_and_title_bold = models.BooleanField(default=False)

    def __str__ (self):
        return self.title

views.py:

def index(request):
    posts= post.objects.filter(active_post=True)
    normal_post = posts.filter(normal_post=True)
    commercial_post = posts.filter(commercial_post=True)
    post_with_border = posts.filter(post_with_colored_border=True)
    post_title_bold = posts.filter(post_title_bold=True)
    post_with_border_and_title_bold = posts.filter(post_with_border_and_title_bold=True)



    context = {
        'posts':posts,
        'commercial_post':commercial_post,
        'post_with_border':post_with_border, 
        'post_title_bold':post_title_bold, 
        'post_with_border_and_title_bold':post_with_border_and_title_bold, 
        }

    return render(request, 'index.html', context)

index.html:

{% if post_with_border AND commercial_post %}
    {% for abc in posts %}         
        <div>
            <a href="#" class="border border-danger">
                <img src="{{abc.picture.url}}">
            </a>
            <h1>
                <a href="#" class="SomeClass"> {{abc.title}}
                </a>
            </h1> 
        </div>
    {% endfor %}

{% else %}  

    {% for abc in normal_post %}         
        <div>
            <a href="#">
                <img src="{{abc.picture.url}}">
            </a>
            <h6>
                <a href="#"> {{abc.title}}
                </a>
            </h6> 
        </div>
    {% endfor %}
{% endif %}

我想让它列出如果它是商业帖子,它应该有一个边框和标题中的 H1,但如果它是一个普通的帖子,它应该没有边框和 H6。但问题是它显示所有带有边框和 H1 的帖子。 我需要你的帮助

谢谢

【问题讨论】:

    标签: django for-loop if-statement conditional-statements


    【解决方案1】:

    你应该考虑重构你的模型。

    models.py

    class post(models.Model):
        title = models.CharField(max_length=40)
        picture = models.ImageField(upload_to='somewhere/dir')
        active_post = models.BooleanField(default=True)
        commercial_post = models.BooleanField(default=False)
    
    def __str__ (self):
        return self.title
    

    views.py

    def index(request):
        posts= post.objects.filter(active_post=True)
    
        context = {
            'posts':posts,
        }
    
        return render(request, 'index.html', context)
    

    index.html

    {% for post in posts %}         
        <div>
            {% if post.commercial_post %}
            <a href="#" class="border border-danger">
                <img src="{{post.picture.url}}">
            </a>
            <h1>
                <a href="#" class="SomeClass"> {{post.title}}
                </a>
            </h1> 
            {% else %}
            <a href="#">
                <img src="{{post.picture.url}}">
            </a>
            <h6>
                <a href="#"> {{post.title}}
                </a>
            </h6> 
            {% endif %}
        </div>
    {% endfor %}
    

    问候

    【讨论】:

      【解决方案2】:

      在循环浏览帖子之前,您正在检查帖子是否是商业的,我认为您想要的是:

      {% for post in posts %}         
          {% if post.commercial_post is True %}
          // border & h1 tags, etc.
          {% else %
          // other stuff
      {% endfor %}
      

      我会更进一步,只使用 if 语句在 div 上设置一个类,然后您可以指定该特定类在 CSS 中的样式 - 这将使您的模板更加简洁。

      此外,如果您的逻辑只是商业帖子具有某种格式,那么您应该删除模型和views.py中的所有其他字段:

      post_with_colored_border = models.BooleanField(default=False)
      post_title_bold = models.BooleanField(default=False)
      post_with_border_and_title_bold = models.BooleanField(default=False)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-21
        相关资源
        最近更新 更多