如何顯示信息,沒有帖子,而不是404錯誤?沒有帖子信息,而是404錯誤
在django中這樣的事情有多頻繁?
def post_by_category(request, slug):
category = get_object_or_404(Category, slug=slug)
categories = category.get_descendants(include_self=True)
posts = get_list_or_404(Post, category__in=categories)
return render_to_response("by_category.html",
{'posts': posts, context_instance=RequestContext(request))
我嘗試使用我的模板:{%if posts%} posts {%else%} No post {%endif%}。它也可以。哪裏做得更好:在視圖中還是在模板中?謝謝 – euro98
如果你使用相同的模板,你應該在模板本身中實現它。 –