【问题标题】:Django 'if not' statement is not working properlyDjango 'if not' 语句无法正常工作
【发布时间】:2016-10-06 17:57:55
【问题描述】:

我正在努力做到这一点,以便在我签署所有草稿时都以红色字母显示 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)

这是我认为的sn-p

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')

为什么它适用于发布而不是'如果不是 post.publish'

【问题讨论】:

  • if post.status == 'draft'
  • @Pythonista 我在看到这篇文章之前就想通了。但是,如果你做到了,我可以给你信用,我会的。不过谢谢

标签: python django conditional


【解决方案1】:

在你的模板中你应该使用:

{% if post.status == 'draft' %}

您的模型上有以下字段:

status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')

您只需检查帖子状态的字段值是否为草稿。

检查post.draft 将不起作用,因为没有字段draft

【讨论】:

    猜你喜欢
    • 2016-04-05
    • 2019-07-23
    • 2015-09-27
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多