for loop确实在view.py Django中工作?

时间:2016-07-21 10:56:41

标签: python django django-views

在view.py

Id = [2,3,4,5,6,7,8]

for w in Id:
    A = w
    Pending = pending(A)
    data = {     
        'Pending': Pending,

    }
    return render_to_response('dialer_campaign/campaign/list.html', data, context_instance=RequestContext(request))

def pending(campaign_id):

    A = Campaign_phonebook.objects.values_list('phonebook_id').filter(campaign_id = campaign_id)

    B = Contact.objects.filter(phonebook_id__in=A).count()
    C = Subscriber.objects.filter(campaign_id = campaign_id).exclude(status = 1).count()
    Result = B - C
    return Result

当我添加手动值而不是A时,它会给出结果,但现在我想通过for循环给出值,它不起作用。为什么?有人可以帮助我吗?

我想在模板中做更改吗?

In image you can see result.it is result when i assign pending(8).I want to get result against loop. Result of every box in pending should be according to value of loop.

先谢谢..

1 个答案:

答案 0 :(得分:2)

将您的数据对象放入列表并渲染到模板

参考以下代码

Id = [2,3,4,5,6,7,8]
pending_list = []
for w in Id:
    pending = pending(w)
    pending_list.append({'pending': pending})
    return render_to_response('dialer_campaign/campaign/list.html', pending_list, context_instance=RequestContext(request))

def pending(campaign_id):
    A = Campaign_phonebook.objects.values_list('phonebook_id').filter(campaign_id = campaign_id)
    B = Contact.objects.filter(phonebook_id__in=A).count()
    C = Subscriber.objects.filter(campaign_id = campaign_id).exclude(status = 1).count()
    Result = B - C
    return Result

在模板中使用pending_list。在pending_list列表中,您将获得所有待处理对象。