django - 2 views in one template

陌路散爱 提交于 2019-12-04 07:40:46

You do not want to have 2 views in 1 template (which is not possible anyway), but have 2 models available in 1 template for rendering. Just do it like this:

draft_list = Post.objects.filter(isdraft=True).order_by("-posted")
publish_list = Post.objects.filter(isdraft=False).order_by("-posted")
return render_to_response('userside/admin.html',
                 {'draft_list':draft_list,'publish_list':publish_list})

From your question, it seems that you're using function based views. An alternative way to solve the problem you're having is to use class based views and override the get_context_data method to pass your template two contexts, one for each queryset.

#views.py
class MyClassBasedView(DetailView):
    context_object_name = 'draft_list'
    template='my-template'
    queryset = Post.objects.filter(isdraft=True).order_by("-posted")

    def get_context_data(self, **kwargs):
       context = super(MyClassBasedView, self).get_context_data(**kwargs)
       context['publish_list'] = Post.objects.filter(isdraft=False).order_by("-posted")
       return context
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!