Nolan M.
Nolan M.

Reputation: 23

Django: Showing "featured" items?

I'm having a little conundrum with sorting some items. I have a field called featured thats a boolean. I'm trying to display the featured coins first and then the remaining will be sorted by a different metric. In this code im using pub_date.

However, when I put an if statement in my template for the featured items it's still showing those that are set to false as well. I'll post code below.

index.html loops and if's

    {% 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 for index

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)

models.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)

How should I go about listing the featured coins that are set to true first and then display the remaining items after?

Upvotes: 1

Views: 868

Answers (1)

bdoubleu
bdoubleu

Reputation: 6107

You need to filter for featured=True in your queryset.

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)

Upvotes: 2

Related Questions