Django Tutorial Part 6 -- polls アプリでテーブルの中身を好きな順番で並べる
5594 단어 django
from django.shortcuts import render
from django.http import HttpResponse
from .models import Question
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:3]
output = ', '.join([q.question_text for q in latest_question_list])
return HttpResponse(output)
질문 테이불을 모델로부터 인포트
<QuerySet [
<Question: What's new?>, <Question: Question 2>,
<Question: Questions 3>, <Question: Questions 4>
]>
Question の objects を作成日順に 3 つまでに制限して取得する.
["What's new?", 'Question 2', 'Questions 3', 'Questions 4']
,
で 先ほどのlistを展開したものを連結する
admin でみると Questinons 4 まである状態でも

最新の 4 에서 2 까지 3 つだけ並んだ.
latest_question_list = Question.objects.order_by('-pub_date')[:4]
これを 4 に変更すると

一番古い、最新から 4 つ目の question_text まで並んだ.
latest_question_list = Question.objects.order_by('pub_date')[:4]
-pub_date
(을)를 pub_date
に変更すると、
古い順で並ぶ.
output = ' | '.join(
[q.question_text for q in latest_question_list]
)
区切り을
|
に変更すると
これで区切られる.view を気にするのはFrontの仕事になるので, こうやってみやすい区切り文字に変更する ことはないと思うが.
다음은 템플릿입니다.
polls/template/index.html を 보기 に組み込み, テーブルのデータを HTML に渡すようにする.
Reference
이 문제에 관하여(Django Tutorial Part 6 -- polls アプリでテーブルの中身を好きな順番で並べる), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/kaede_io/django-tutorial-part-6-polls-no-index-de-questiontext-wobing-beru-1log텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)