Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 嵌套多个';对于';循环_Django_Python 3.x_Django Models_Django Templates_Django Views - Fatal编程技术网

Django 嵌套多个';对于';循环

Django 嵌套多个';对于';循环,django,python-3.x,django-models,django-templates,django-views,Django,Python 3.x,Django Models,Django Templates,Django Views,我使用的是Django 1.8.3和Python 3.4.3 我的应用程序开始变大,我有几个“for”循环来访问同一模型中的多个对象。虽然我是Python新手,但我猜想有一种更为精简的方法来准备我的代码。请看下面的小片段。该模型是“一周中的一天”,但我有一个针对多个对象的“for”循环。我还放置了一段完整图片的模板代码。谢谢你的帮助 views.py def get_context_data(self, **kwargs): context = super(EmailListVi

我使用的是Django 1.8.3和Python 3.4.3

我的应用程序开始变大,我有几个“for”循环来访问同一模型中的多个对象。虽然我是Python新手,但我猜想有一种更为精简的方法来准备我的代码。请看下面的小片段。该模型是“一周中的一天”,但我有一个针对多个对象的“for”循环。我还放置了一段完整图片的模板代码。谢谢你的帮助

views.py

def get_context_data(self, **kwargs):
        context = super(EmailListView, self).get_context_data(**kwargs)
        days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
        months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
                  'November', 'December']
        subject_type = ['Offer', 'Sell', 'Fear', 'Learn']
        content_type = ['Offer', 'Sell', 'Fear', 'Learn']
        email_list = ['FMGB', 'FMGR', 'AE', 'IBA']

        total_campaigns = {}
        total_campaigns_month = {}
        total_recipients = {}
        total_unsubscribes = {}
        total_bounces = {}
        total_open = {}
        total_clicks = {}

        for day in days:
            total_campaigns[day] = Email.objects.filter(day_of_week=day).count()
        for recipients in days:
            total_recipients[recipients] = Email.objects.filter(day_of_week=recipients).aggregate(
                Sum('recipients')).get('recipients__sum', 0.00)
        for unsubscribes in days:
            total_unsubscribes[unsubscribes] = Email.objects.filter(day_of_week=unsubscribes).aggregate(
                Sum('unsubscribes')).get('unsubscribes__sum', 0.00)
        for bounces in days:
            total_bounces[bounces] = Email.objects.filter(day_of_week=bounces).aggregate(Sum('bounces')).get(
                'bounces__sum', 0.00)
        for open in days:
            total_open[open] = Email.objects.filter(day_of_week=open).aggregate(
                Sum('open')).get('open__sum', 0.00)
        for clicks in days:
            total_clicks[clicks] = Email.objects.filter(day_of_week=clicks).aggregate(
                Sum('clicks')).get('clicks__sum', 0.00)
模板片段。。。(email.html)

{%if email\u list%}
星期一
{{total_.Monday}
{{total_recipients.Monday}
{{total_unsubscribes.Monday}
{{total_bounces.Monday}
{{total_open.Monday}
{{total_clicks.Monday}
{%average total_open.Monday total_recipients.Monday%}
{%average total_clicks.Monday total_open.Monday%}
...

这应该做同样的事情:

for day in days:
    total_campaigns[day] = Email.objects.filter(day_of_week=day).count()
    # recipients
    total_recipients[day] = Email.objects.filter(day_of_week=day).aggregate(
        Sum('recipients')).get('recipients__sum', 0.00)
    # unsubscribes
    total_unsubscribes[day] = Email.objects.filter(day_of_week=day).aggregate(
        Sum('unsubscribes')).get('unsubscribes__sum', 0.00)
    # bounces
    total_bounces[day] = Email.objects.filter(day_of_week=day).aggregate(
        Sum('bounces')).get('bounces__sum', 0.00)
    # open
    total_open[day] = Email.objects.filter(day_of_week=day).aggregate(
        Sum('open')).get('open__sum', 0.00)
    # clicks
    total_clicks[day] = Email.objects.filter(day_of_week=day).aggregate(
        Sum('clicks')).get('clicks__sum', 0.00)
我对django ORM不太熟悉,但您可能可以使用

for day in days:
    day_objects = Email.objects.filter(day_of_week=day)
    total_campaigns[day] = day_objects.count()
    # recipients
    ...

…etc(并用新定义的
day\u对象替换所有
Email.objects.filter(day\u of_week=day)

所以我不确定这是否都是正确的语法,但也许您可以尝试这种方法

def initialize_dict(dict, days, dict_name):
    for day in days:
        if dict_name='':
            dict[day] = Email.objects.filter(day_of_week=day).count()
        else:
            dict[day] = Email.objects.filter(day_of_week=day)
                       .aggregate(Sum(dict_name))
                       .get(dict_name + '__sum', 0.00)
我不确定这是否是一个更好的解决方案,但这是一个替代方案。您可以决定将天数作为全局变量,而不必将其传递到函数中。以下是几个示例调用:

initialize_dict(total_campaigns, days, '')
initialize_dict(total_recipients, days, 'recipients')

等等。。。我希望这有帮助。请随意评论这个解决方案,解释它是好方法还是坏方法!我仍在学习和改进自己:)

您能否创建一个名为“initialize”(或类似的东西)的函数,它接受您试图迭代的内容以及任何附加变量,以便将重复代码精简为单个for循环?这有意义吗?编辑:我觉得下面的第一个答案不错。我的建议需要使用不同的参数调用相同的方法。谢谢你的建议。在我开始实施这个解决方案之前,如果你能给这个noob提供一个使用不同参数的建议的例子,这会不会太过分了?谢谢。当然,我会尝试一个简单的例子,但我警告你,我也是一个noob:)。这非常有效-摆脱所有额外的“for”语句非常好非常感谢。祝你周末愉快。谢谢你抽出时间来帮助我。我选择了《希罗英雄》的解决方案,因为它似乎更容易让我理解,尤其是当我还在整理这一切的时候:)再次感谢你,祝你周末愉快。没问题!祝你周末愉快。
initialize_dict(total_campaigns, days, '')
initialize_dict(total_recipients, days, 'recipients')