获得“为关键字参数获取多个值”错误

时间:2014-01-01 00:32:49

标签: python django django-views

所以我要生成一份为期两周的过期域名报告。它会每天通过电子邮件发送。我已经陷入了一个特定的问题,我已经遇到了几天,并且不能为我的生活弄清楚它为什么会发生。错误是" email_report()获得了关键字参数' duration'"的多个值。这是代码(它很难看,我知道)..

这是我的网址:

url(r'^reports/domains/(?P<duration>\d+)(?P<unit>\w)/emailreport/$', email_report),

这是我的逻辑和观点:

def expiring_domains_logic(unit):
    report_date = date.today()
    unit = unit.lower()
    if unit == 'd': report_date = report_date + timedelta(days = int(duration))
    elif unit == 'w': report_date = report_date + timedelta(weeks = int(duration))
    elif unit == 'm': report_date = report_date + timedelta(weeks = int(duration)*4)
    elif unit == 'y': report_date = report_date + timedelta(weeks = int(duration)*52)

    domains = DomainMain.objects.filter(nextrenewaldate__lte = report_date).order_by('registrarmultiid')
    cost = count_cost(domains)
    registrars = Registrars.objects.all()
    return (domains, report_date, cost, registrars)

def expiring_domains(request = None, duration = 2, unit = 'w'):
    (domains, report_date, cost, registrars) = expiring_domains_logic(unit)
    if request == None:
        return render_to_string('reports/expiring_domains.html', { 'domains': domains, 'expiration_date': report_date, 'cost': cost, 'registrars': registrars })
    else:
        return render(request, 'reports/expiring_domains.html', { 'domains': domains, 'expiration_date': report_date, 'cost': cost, 'registrars': registrars  })

def email_report(duration, unit):
    domains = expiring_domains(duration, unit) # two weeks
    email_targets = Owners.objects.filter(emailflag = 'Y')
    email_targets = (x.coemail for x in email_targets)
    email_body = domains.content # This is where we put together the email body
    msg = mail.EmailMessage('subject_here', email_body, 'no-reply@[omitted]', email_targets)
    msg.content_subtype = "html"
    msg.send()

回溯:

File "/home/ashley/enviro/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
Exception Type: TypeError at /reports/domains/2w/emailreport/
Exception Value: email_report() got multiple values for keyword argument 'duration'

1 个答案:

答案 0 :(得分:3)

您的网址将email_report定义为视图,但实际上看起来根本不是视图 - 它不接受请求并返回响应。该视图似乎称为expiring_domains。