2017-03-08 36 views
0

私のクエリーセットは現在、私の見解ではpostsの値です。しかし、ユーザーがnewのURLに移動すると、クエリーセットをPost.objects.all().filter(category=category).order_by('-date')に変更したいと思います。ここに私のコードは次のとおりです。ビュー内の変数の値を変更する方法は?

のURL

url(r'^$', boxes_view, name='home'), 
url(r'^new/$', boxes_view, name='new'), 

ビュー

def boxes_view(request): 
    ... 
    posts = Post.objects.all().filter(category=category).annotate(score_diff=F('score__upvotes') - F('score__downvotes')).order_by('-score_diff') 

テンプレート

... 
<button id="sort_new"><a href="{% url 'new' %}">New</a></button> 

new URLを経由postsの値を変更することが可能ですか?

+0

これは意味がありません。 2つの同一のURLを持つことはできません。 –

+0

私の間違いは修正されました。 – Zorgan

答えて

4

あなたがしようとしていることは本当に分かりません。しかし、urls.pyのパラメータをビューに渡すことができます。

url(r'^$', boxes_view, {'order': '-score_diff'}, name='home'), 
url(r'^new/$', boxes_view, {'order': '-date'}, name='new'), 

def boxes_view(request, order): 
    ... 
    posts = Post.objects.all()...order_by(order) 
+0

これは正しい方法ですが、試しましたが、私の 'new' URLは空のクエリーセットを返しています。どんな考え?私は 'posts'のforループを使用し、/ new / – Zorgan

関連する問題