jeudi 7 mai 2020

a post with border and no border using for or if in django

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:

  

    

i want to make it to list if its a commercial post it should have a border and a H1 in title but if its a normal post it should have no border and H6. But the problem is it shows all posts with border and H1. I need your help please

Thank you

Aucun commentaire:

Enregistrer un commentaire