2016-08-17 50 views
2

更新到django 1.10後,處理器停止工作。Django 1.10 RequestContext處理器不工作

了很多意見,此菜單背景:

def menu_category(request): 
    category_parent = Category.objects.filter(parent__isnull=True, is_active=True).order_by('mass') 
    category_child = Category.objects.filter(parent__isnull=False, is_active=True).order_by('mass') 
    return {'category_parent': category_parent, 'category_child': category_child} 

這一觀點Django的1.9(在Django 1.10處理器= [menu_category]空的情況下):

def news_main(request): 
    posts = Post.objects.filter(
     Q(date_completion__gt=timezone.now()) | Q(date_completion=None), 
     date_published__lte=timezone.now(), 
     is_active=True).order_by('-date_published') 
    return render_to_response('news/news_main.html', 
    {'posts': posts}, 
    RequestContext(request, processors=[menu_category])) 

回答

5

你永遠不應該被傳遞RequestContext到render_to_response,Django 1.10已經發生了錯誤。而是使用render快捷:

return render(request, 'news/news_main.html', {'posts': posts}) 

請注意,只要你升級版本,你應該始終確保閱讀發佈說明;在這種情況下,更改在Features removed in 1.10下注明。

+0

那麼,我可以在模板中添加上下文menu_category嗎? –

+0

自動應用它,只要它在TEMPLATES設置的'context_processors'選項中列出。 –