I'm trying to make it so that when I am signed in all my drafts show the word DRAFT in red letters so I tried the following
<h6 id="date">{% if post.draft %}<div id="draft">DRAFT</div>{% endif %} </h6>
but it's not working but if I do
<h6 id="date">{% if post.publish %}<div id="draft">DRAFT</div>{% endif %} </h6>
it works, and since that worked I figured using 'not' would work
<h6 id="date">{% if not post.publish %}<div id="draft">DRAFT</div>{% endif %} </h6>
but that didn't work. heres my model
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)
title = models.CharField(max_length=250, unique=True)
slug = models.SlugField(max_length=250,
unique_for_date='publish')
image = models.ImageField(upload_to=upload_location,
null=True,
blank=True,
height_field='height_field',
width_field='width_field')
image_url = models.CharField(max_length=500,
null=True,
blank=True,
)
height_field = models.IntegerField(default=0,
null=True,
blank=True,
)
width_field = models.IntegerField(default=0,
null=True,
blank=True,
)
author = models.ForeignKey(User,
related_name='blog_posts',
null=True,
blank=True,)
body = models.TextField(null=True, blank=True,)
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10,
choices=STATUS_CHOICES,
default='draft')
video = models.BooleanField(default=False)
video_path = models.CharField(max_length=320,
null=True,
blank=True,)
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('blog:post_detail', kwargs={"slug": self.slug})
objects = models.Manager() # The default manager.
published = PublishedManager() # Our custom manager.
tags = TaggableManager(blank=True)
and here is the the snippet in my view
if request.user.is_staff or request.user.is_superuser:
object_list = Post.objects.all().order_by('-id')
else:
object_list = Post.published.all().order_by('-id')
why is it working with publish but not 'if not post.publish'
In your template you should use:
{% if post.status == 'draft' %}
You have the following field on your model:
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
You need only check if the post status' field value is draft or not.
Checking post.draft
won't work, because there is no field draft
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.