1

我有一个来自 django-registration 的功能,它将激活电子邮件重新发送给给定的收件人。我正在尝试将该功能从接受给定电子邮件的多个用户转换为每封电子邮件仅接受一个用户。但是,AttributeError当我尝试更改它时它会抛出一个。

def resend_activation(self, email, site):   # for multiple emails -- this works
    sent = False
    users = User.objects.all().filter(email=email)

    if users:
        for user in users:
            registration_profiles = self.all().filter(user=user)
            for registration_profile in registration_profiles:
                if not registration_profile.activation_key_expired():
                    registration_profile.send_activation_email(site)
                    sent = True
    return sent

def resend_activation(self, email, site):   # for single email -- this does not work
    sent = False                            
    user = User.objects.all().filter(email=email)

    if user:
        registration_profile = self.all().get(user=user)
        if not registration_profile.activation_key_expired():
            registration_profile.send_activation_email(site)
            sent = True
    return sent

后一个函数抛出一个AttributeError,但我无法弄清楚为什么没有for循环该函数将无法“工作”。我这里的问题似乎是什么?谢谢你。

4

1 回答 1

4

尝试:

def resend_activation(self, email, site):
    sent = False
    # Get the user you are looking for
    try:
        single_user = User.objects.get(email=email)
    except User.DoesNotExist:
        return false

    # Get all the profiles for that single user
    registration_profiles = self.all().filter(user=user)
        # Loop through, and send an email to each of the profiles belonging to that user
        for registration_profile in registration_profiles:
            if not registration_profile.activation_key_expired():
                registration_profile.send_activation_email(site)
                sent = True
    return sent

在原始版本中, User.object.filter(email=email) 返回一个查询集,它是从查询filter(email=email)返回的数据库中的对象集合。原文中的 for 循环是遍历这些对象中的每一个,并发送相应的电子邮件。你试图打电话给 send_

于 2011-06-03T18:15:22.113 回答