簡體   English   中英

Django'if not'語句無法正常工作

[英]Django 'if not' statement is not working properly

我正在嘗試這樣做,以便當我在所有草稿上簽名時,用紅色字母顯示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')

為什么它可以與發布一起工作,但不能“如果不是post.publish”

在模板中,您應該使用:

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

您的模型上有以下字段:

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

您只需要檢查帖子狀態的字段值是否為草稿。

由於沒有字段draft ,因此無法檢查post.draft

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM