I have a django queryset in my views whose values I pack before passing to my template. There is a problem when the queryset returns none since associated values are not unpacked. the quersyet is called comments.
Here is my views.py
def forums(request ):
post_list = list(forum.objects.filter(child='0')&forum.objects.filter(deleted='0').order_by('postDate'))
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)
comments =list( forum.objects.filter(deleted='0').filter(child='1').order_by('childParentId').values('childParentId').annotate(y=Count('childParentId')))
if request.user.is_staff== True :
staff = 1
else:
staff = 0
staffis = 1
if newpostform.is_valid():
topic = request.POST['postSubject']
poster = request.POST['postPoster']
newpostform.save()
return HttpResponseRedirect('/forums')
else:
newpostform = PostForm(initial = {'postPoster':user.id})
if request.GET:
form = SearchForm(request.GET)
if form.is_valid():
query = form.cleaned_data['query']
post_list = list((forum.objects.filter(child='0')&forum.objects.filter(deleted='0')&forum.objects.filter(Q(postSubject__icontains=query)|Q(postBody__icontains=query)|Q(postDate__icontains=query)))or(forum.objects.filter(deleted='0')&forum.objects.filter(Q(postSubject__icontains=query)|Q(postBody__icontains=query)|Q(postDate__icontains=query)).values('childParentId')))
if request.method == 'POST':
delpostformset = DelPostFormSet(request.POST)
if delpostformset.is_valid():
delpostformset.save()
return HttpResponseRedirect('/forums')
else:
delpostformset = DelPostFormSet(queryset=forum.objects.filter(child='0', deleted='0'))
"""if readform.is_valid():
user=get_object_or_404(UserProfile.objects.all())
readform.save()
else:
readform = ReadForumForm()"""
post= zip( post_list,comments, delpostformset.forms)
paginator = Paginator(post, 10) # Show 10 contacts per page
# Make sure page request is an int. If not, deliver first page.
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
# If page request (9999) is out of range, deliver last page of results.
try:
post = paginator.page(page)
except (EmptyPage, InvalidPage):
post = paginator.page(paginator.num_pages)
return render_to_response('forum.html', {'post':post, 'newpostform': newpostform,'delpost':delpostformset, 'username':user.username, 'comments':comments, 'user':user, },context_instance = RequestContext( request ))