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
cheers
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" %}
On Fri, Oct 19, 2018 at 10:13 PM Joel <joel@eyrie.in> wrote:
--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.On Sat, 20 Oct, 2018, 9:20 AM Daniel Veazey, <danielveazey@gmail.com> wrote: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 contextThen 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?
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CAA%3Diw_-Cu%2B-NtdGZeY9fC2VJ%2B13h4YWUyMY181Vez9E5E8C0Vw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
att.
Carlos Rocha
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CAM-7rO3t2POcaCmi-2K25bC33_v%3DLwPcVv0m27DyZbJMV0%3D92Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment