好的,这是我想出来的解决方案。虽然可能不太美观,但对我有用。
这是加入到views.py中的内容。
from .forms import ResendActivationEmailForm
from django.core import signing
from django.contrib.sites.shortcuts import get_current_site
from django.template.loader import render_to_string
def resend_activation_email(request):
email_body_template = 'registration/activation_email.txt'
email_subject_template = 'registration/activation_email_subject.txt'
if not request.user.is_anonymous():
return HttpResponseRedirect('/')
context = Context()
form = None
if request.method == 'POST':
form = ResendActivationEmailForm(request.POST)
if form.is_valid():
email = form.cleaned_data["email"]
users = User.objects.filter(email=email, is_active=0)
if not users.count():
form._errors["email"] = ["Account for email address is not registered or already activated."]
REGISTRATION_SALT = getattr(settings, 'REGISTRATION_SALT', 'registration')
for user in users:
activation_key = signing.dumps(
obj=getattr(user, user.USERNAME_FIELD),
salt=REGISTRATION_SALT,
)
context = {}
context['activation_key'] = activation_key
context['expiration_days'] = settings.ACCOUNT_ACTIVATION_DAYS
context['site'] = get_current_site(request)
subject = render_to_string(email_subject_template,
context)
subject = ''.join(subject.splitlines())
message = render_to_string(email_body_template,
context)
user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)
return render(request, 'registration/resend_activation_email_done.html')
if not form:
form = ResendActivationEmailForm()
context.update({"form" : form})
return render(request, 'registration/resend_activation_email_form.html', context)
这是添加到forms.py中的内容。
class ResendActivationEmailForm(forms.Form):
email = forms.EmailField(required=True)
我在注册时添加了一个名为resend_activation_email_form.html的新模板,当邮件被重新发送时会使用它。但是我使用了完全相同的模板来发送邮件。
我之所以基于电子邮件,是因为我使用了唯一的电子邮件,但更明智的做法可能是基于用户名,因为django-registration定义它为唯一的。
from registration.models import RegistrationProfile
时,我会收到错误提示 "Table 'mydb.registration_registrationprofile' doesn't exist"。 - HenryM