python - Django:显示 "featured"项目?

标签 python django python-3.x django-models django-templates

我在对某些项目进行排序时遇到了一些难题。我有一个名为“featured”的字段,它是一个 bool 值。我试图首先显示特色硬币,然后其余的将按不同的指标排序。在此代码中,我使用 pub_date。

但是,当我在模板中为特色项目添加 if 语句时,它仍然显示那些设置为 false 的项目。我将在下面发布代码。

index.html 循环和 if

    {% if featured_coins_list %}
            {% for coin in featured_coins_list %}
            <div class="large-6 medium-6 cell">
                <h2><a href="/coins/{{ coin.id }}/">{{ coin.name }}</a></h2>
                <p>Ticker: {{ coin.ticker }}</p>
                <p>{{ coin.summary }}</p>
                <a href="/coins/{{ coin.id }}" class="button">More Info</a></strong>
            </div>
        {% endfor %}
        {% endif %}

        {% if latest_coins_list %}
          {% for coin in latest_coins_list %}
            <div class="large-6 medium-6 cell">
              <h2><a href="/coins/{{ coin.id }}/">{{ coin.name }}</a></h2>
              <p>Ticker: {{ coin.ticker }}</p>
              <p>{{ coin.summary }}</p>
              <a href="/coins/{{ coin.id }}" class="button">More Info</a></strong>
            </div>
            {% endfor %}
          </div>
{% else %}
    <p>No coins are available.</p>
{% endif %}

索引的views.py

def index(request):
    featured_coins_list = Coin.objects.order_by('-featured')[:4]
    latest_coins_list = Coin.objects.order_by('-pub_date')[:8]
    context = {'featured_coins_list': featured_coins_list,
               'latest_coins_list': latest_coins_list}
    return render(request, 'coins/index.html', context)

模型.py

class Coin(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=100)
    ticker = models.CharField(max_length=5)
    featured = models.BooleanField(default=False)
    logo = models.ImageField(upload_to='uploads/', verbose_name='image')
    website = models.URLField(max_length=200, default="https://example.com/")
    reddit = models.URLField(max_length=200, default="https://reddit.com/r/")
    twitter = models.URLField(max_length=200, default="https://twitter.com/")
    summary = models.CharField(max_length=500, blank=True)
    description = models.TextField()
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.ticker

    def is_featured(self):
        return self.featured

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

我应该如何列出首先设置为 true 的特色硬币,然后显示其余项目?

最佳答案

您需要在查询集中过滤featured=True

def index(request):
    featured_coins_list = Coin.objects.filter(featured=True).order_by('-featured')[:4]
    latest_coins_list = Coin.objects.order_by('-pub_date')[:8]
    context = {'featured_coins_list': featured_coins_list,
               'latest_coins_list': latest_coins_list}
    return render(request, 'coins/index.html', context)

关于python - Django:显示 "featured"项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50312181/

相关文章:

python - 从 Django 中的表单字段获取文件

python - mongodb 或 redis 用于类似 facebook 的网站?

python - Jenkins 构建的 Django 应用程序的 Docker 容器中的错误时间

python-3.x - Pygame set_alpha 不适用于尝试的背景淡入淡出

python - 如何在不删除分隔符的情况下从字符串中获取唯一值

python - 在python中动态获取类级别变量的类名

Python Pandas - 将多个电子表格中的特定选项卡合并为一个

django -/ 'utf8' 处的 UnicodeDecodeError 编解码器无法解码字节

python - 突然出现导入错误

python-3.x - RuntimeError : You must compile your model before using it