我有 2 个要在模板中呈现的数组,一个是要输出的数据,另一个是用于删除项目的表单集。由于 django 似乎不支持模板标签中的布尔运算符,我尝试打包项目,但它们仅返回 2 行中的第一个项目和第一个表单。
如何打包这些项目,以便它们在一个 for 循环中呈现。
我的观点
@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, })
我的模板
<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>