2017-03-08 49 views
0

我查詢集是目前posts我認爲值。不過,我想,當用戶導航到new URL我的查詢集更改爲Post.objects.all().filter(category=category).order_by('-date')。這裏是我的代碼:如何更改視圖中變量的值?

網址

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> 

是否有可能改變的posts值通過new URL會是什麼時候?

+0

這沒有任何意義。你不能有兩個相同的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'網址傳回一個空的查詢集。任何想法爲什麼?我在'posts'上使用了一個for循環,並且它在/ new / – Zorgan