views:

103

answers:

1

I have 2 arrays that I would like to render in a template, one is the data to be output, the other is a formset for deleting items. since it seems django does not support boolean operators in the template tag, I have tried packaging the items, but they return the first item and the first form in 2 rows only.

How does one package such items so that they are rendered in one for loop.

my view

@login_required
def forums(request ):

    post = list(forum.objects.filter(child='0')&forum.objects.filter(deleted='0'))
    user = UserProfile.objects.get(pk=request.session['_auth_user_id'])
    newpostform = PostForm(request.POST)
    deletepostform = PostDeleteForm(request.POST)
    DelPostFormSet = modelformset_factory(forum, exclude=('child','postSubject','postBody','postPoster','postDate','childParentId'))
    readform = ReadForumForm(request.POST)
    if newpostform.is_valid():
        topic = request.POST['postSubject']
        poster = request.POST['postPoster']
        newpostform.save()
    newpostform = PostForm(initial = {'postPoster':user.id})
    post = list(forum.objects.filter(child='0')&forum.objects.filter(deleted='0'))

    else:
        newpostform = PostForm(initial = {'postPoster':user.id})

    if request.method == 'POST':
        delpostformset = DelPostFormSet(request.POST)
        if delpostformset.is_valid():
            delpostformset.save()

    else:
        delpostformset = DelPostFormSet(queryset=forum.objects.filter(child='0', deleted='0'))


    """if readform.is_valid():
        readform.save()
    else:
        readform = ReadForumForm()"""


    return render_to_response('forum.html', {'post':( post,delpostformset.forms), 'newpostform': newpostform, })

my Template

<table class="results">
<tr class="inner_results_header"><td >Title</td><td>Date/Time</td><td>Poster</td>      <td>Body</td><td><form method="post" id="form" action=""  class="usertabs accfrm"><input type="submit" value="Delete" /></td></tr>  
{{formset.management_form}}
{% for p, form in post   %}



{% url forum_view p.postID  as post_url%} 
<tr  class="inner_results {% if forloop.counter|divisibleby:2 %}evens{% else %}odds{% endif %}"><span onclick="document.location.href='{{post_url}}';"><td>{{ p.postSubject}}</td><td>{{p.postDate}}</td><td>{{ p.postPoster}}</td><td>{{ p.postBody|truncatewords:50}}</td></span><td>


{{ form.as_p }}


</td></tr> 

{% endfor %}

<tr class="inner_results_header"><td >Title</td><td>Date/Time</td><td>Poster</td> <td>Body</td><td><input type="submit" value="Delete" /></form></td></tr>  

+7  A: 

Use zip builtin. If both post and delpostformset.forms are iterables, zip will return a list of tuples. In view:

post_and_form = zip(post, delpostformset.forms)

and in template:

{% for post, form in post_and_form %}
{% endfor %}
gruszczy