Django 'if not' 语句无法正常工作
Django 'if not' statement is not working properly
我正在尝试做到这一点,以便当我在所有草稿中签名时都以红色字母显示“草稿”一词,所以我尝试了以下
<h6 id="date">{% if post.draft %}<div id="draft">DRAFT</div>{% endif %} </h6>
但它不起作用,但如果我这样做
<h6 id="date">{% if post.publish %}<div id="draft">DRAFT</div>{% endif %} </h6>
它有效,因为它有效,我认为使用 'not' 会有效
<h6 id="date">{% if not post.publish %}<div id="draft">DRAFT</div>{% endif %} </h6>
但这没有用。这是我的模特
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)
这是我认为的片段
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')
为什么它与 publish 一起工作而不是 'if not post.publish'
在您的模板中,您应该使用:
{% if post.status == 'draft' %}
您的模型中有以下字段:
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
您只需要检查 post 状态字段值是否为草稿。
检查 post.draft
将不起作用,因为没有字段 draft
。
我正在尝试做到这一点,以便当我在所有草稿中签名时都以红色字母显示“草稿”一词,所以我尝试了以下
<h6 id="date">{% if post.draft %}<div id="draft">DRAFT</div>{% endif %} </h6>
但它不起作用,但如果我这样做
<h6 id="date">{% if post.publish %}<div id="draft">DRAFT</div>{% endif %} </h6>
它有效,因为它有效,我认为使用 'not' 会有效
<h6 id="date">{% if not post.publish %}<div id="draft">DRAFT</div>{% endif %} </h6>
但这没有用。这是我的模特
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)
这是我认为的片段
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')
为什么它与 publish 一起工作而不是 'if not post.publish'
在您的模板中,您应该使用:
{% if post.status == 'draft' %}
您的模型中有以下字段:
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
您只需要检查 post 状态字段值是否为草稿。
检查 post.draft
将不起作用,因为没有字段 draft
。