Reputation: 418
So I am trying to create some moderation in my app. A users post should be false when the create it and a moderator must come in and set it to true for it to go live.
I have added the field into my models. But I am struggling to get the true ones to display on my template.
MODELS:
class Post(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE, blank=True, null=True)
image = models.ImageField(blank=True, null=True)
live = models.BooleanField(default=False)
VIEWS:
class IndexView(ListView):
model = Post
template_name = "public/index.html"
I know I need to use an if statement but I am not sure how to implement it. Thanks.
Upvotes: 1
Views: 276
Reputation: 392
You have to use an if statement in the template.
{% for object in object_list %}
{% if object.live %}
<div>
// Here you can put your Post
</div>
{% endif %}
{% endfor %}
Upvotes: 0
Reputation: 600059
A better way would be to override the queryset in the view to only fetch live posts.
class IndexView(ListView):
queryset = Post.objects.filter(live=True)
template_name = "public/index.html"
Now you don't need to change the template at all.
Upvotes: 1