Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django - 2 views in one template

Tags:

django-views

i have 2 different views which are getting filtered data from db. and i have to use these views in one template file(admin.html) but i cant use multiple views on a page at same time.

here is my view1:

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

view2 :

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

i d like to use them like :

{% for d in draft_list %}
{{ d.title }}
{% endfor %}

--

{% for p in publish_list %}
{{ p.title }}
{% endfor %}

i want to make these 2 views 'one view' .what is the correct way?

like image 348
alioguzhan Avatar asked Aug 20 '12 23:08

alioguzhan


1 Answers

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})
like image 62
schacki Avatar answered Sep 22 '22 05:09

schacki