2016-07-15 16 views
0

私のホームページには私のブログのすべての記事が展示されていますが、最も古い投稿から最新のものへと間違って並べ替えられています。 は、私は私のviews.pyに掲載された日付でホームページの投稿順序が正しくない[Django app]

def home(request): 
    posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date') 
    return render(request, "home.html", {'posts': posts}) 

を投稿順にソートするクエリセットを使用し、それは私のhome.htmlのソースコードです:

{% extends "C:\myapp\blog\templates\base.html" %} 
 
{% block content %} 
 
\t {% for post in posts %} 
 
\t \t <div class="post"> 
 
\t \t \t <div class="date"> 
 
\t \t \t \t {{ post.published_date }} 
 
\t \t \t </div> 
 
\t \t \t <h1><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></h1> 
 
\t \t \t 
 
\t \t \t <p>{{ post.text|linebreaksbr }}</p> 
 
\t \t </div> 
 
\t {% endfor %} 
 
{% endblock content %}

あなたは私を助けてもらえこれらの記事を逆にする? ありがとうございます。

答えて

0

order_byの文字列引数に-を追加すると、クエリセットが降順になります。

def home(request): 
    posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date') 
    return render(request, "home.html", {'posts': posts}) 

お知らせ.order_by('-published_date')

+0

おかげでたくさんの仲間!すべてが今クリアされます。 – dannyxn

+0

@ dannyxnこの回答があなたを答えとして受け入れることによって私を助けてくれるのを助けてくれたならば。答えの左側にあるチェックをクリックすると、そうすることができます。 – marcusshep

+0

いいえもう1つ質問がありますが、どうすればよいですか? – dannyxn

関連する問題