Displaying items differently on one template based on age

29 views
Skip to first unread message

Daniel Veazey

unread,
Oct 19, 2018, 11:49:28 PM10/19/18
to Django users
I'm using 2.1. I have a list of blog posts, and I want to display them differently based on their age. The most recent post will be displayed prominently, and the 10 most recent posts after that will be displayed in two columns below it. The way I'm passing the view to the template is:

class PostListView(ListView):
    model
= Post
    template_name
= 'mainapp/home.html'


   
def get_context_data(self, **kwargs):
        context
= super().get_context_data(**kwargs)
        context
['latest'] = Post.objects.all().order_by('-date_posted')[0]
        context
['first_column'] = Post.objects.all().order_by('-date_posted')[1:6]
        context
['second_column'] = Post.objects.all().order_by('-date_posted')[6:10]
       
return context

Then I use 'latest', 'first_column' and 'second_column' in the template to display those things in their separate divs.

Doing it this way works, but I am very new and I think I might have bodged it when there's a better way to do it. Any suggestions?

Daniel Veazey

unread,
Oct 20, 2018, 12:03:19 AM10/20/18
to Django users
If it helps, my template looks like this:

{% extends "photomanager/base.html" %}
{% block content %}
<div class="row">
 
<div class="col-md-4">
   
<!-- sidebar would go here -->
 
</div>
  <div class="col-md-8">
    {% if latest.photo %}
      <a href="{% url 'post-detail' latest.slug %}"><img src="{{ latest.photo.picture.url }}" alt="{{ latest.photo.title }}" class="img-fluid"></
a>
     
<p>Photographer: {{ latest.photo.photographer }}</p>
     
<p>Caption: {{ latest.photo.caption }}</p>
   
{% endif %}
   
<h2><a href="{% url 'post-detail' latest.slug %}">{{ latest.title }}</a></h2>
   
<div class="">
     
{{ latest.content | safe }}
     
<hr>
   
</div>
    <div class="row">
      <div class="col-md-6 col-sm-12">
        {% for post in first_column %}
          {% if post.photo %}
            <a href="{% url 'post-detail' post.slug %}"><img src="{{ post.photo.picture.url }}" alt="{{ post.photo.title }}" class="img-fluid"></
a>
         
{% endif %}
         
<h3><a href="{% url 'post-detail' post.slug %}">{{ post.title }}</a></h3>
         
<div class="">
           
<hr>
         
</div>
        {% endfor %}
      </
div>
     
<div class="col-md-6 col-sm-12">
       
{% for post in second_column %}
         
{% if post.photo %}
           
<a href="{% url 'post-detail' post.slug %}"><img src="{{ post.photo.picture.url }}" alt="{{ post.photo.title }}" class="img-fluid"></a>
         
{% endif %}
         
<h3><a href="{% url 'post-detail' post.slug %}">{{ post.title }}</a></h3>
         
<div class="">
           
<hr>
         
</div>
        {% endfor %}
      </
div>
   
</div>
  </
div>
</div>
{% endblock content %}



Joel

unread,
Oct 20, 2018, 12:13:35 AM10/20/18
to django...@googlegroups.com
You're using three different database queries. You could just fetch the most recent 11 objects,  into a variable, use [0] for the most recent one. And then display the next 10 and break them into columns with css wrap.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/83be2580-6e11-4b99-b854-04aa47486480%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

carlos

unread,
Oct 20, 2018, 12:29:52 AM10/20/18
to django...@googlegroups.com
yes Joel is right you hit 3 time de DB is not good for optimization create a variable and save the one query
def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        POSTS = Post.objects.all().order_by('-date_posted')
        context['latest'] = POSTS[0]
        context['first_column'] = POSTS[1:6]
        context['second_column'] = POSTS[6:10]
        return context

or in your templete use the templatetags slice


only 1 query
context['latest'] = Post.objects.all().order_by('-date_posted')
in the template
{% for post in latest|slice:"0" %}
{% for post in latest|slice:"1:6" %}
{% for post in latest|slice:"6:10" %}

cheers




For more options, visit https://groups.google.com/d/optout.


--
att.
Carlos Rocha

Daniel Veazey

unread,
Oct 20, 2018, 5:35:01 AM10/20/18
to Django users
Ah, very good. I understand now. Thank you both for your help.
Reply all
Reply to author
Forward
0 new messages