上两篇文档解释了这段代码中的两个疑问,order_by('-pub_date')[:5]和[q.question_text for q in latest_question_list]

我们还可以指定逆向排序,在前面加一个减号 - 前缀:

?

1

2

>>> Publisher.objects.order_by("-name")

[<Publisher: O'Reilly>, <Publisher: Apress>]

from django.http import HttpResponse

from .models import Question


def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    output = ', '.join([q.question_text for q in latest_question_list])
    return HttpResponse(output)

# Leave the rest of the views (detail, results, vote) unchanged

逆向排序,

选择数组中的几个元素, 

列表解析式,

https://docs.djangoproject.com/zh-hans/2.1/intro/tutorial03/#write-views-that-actually-do-something

扫描二维码关注公众号,回复: 3637636 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_27361945/article/details/83014316