Django:重新发送激活链接视图
我正在尝试在登录页面上添加“重新发送激活链接”的链接 重发激活链接的模板如下:Django:重新发送激活链接视图,django,django-authentication,Django,Django Authentication,我正在尝试在登录页面上添加“重新发送激活链接”的链接 重发激活链接的模板如下: {% extends 'base.html' %} {% block title %}Forgot Your Password?{% endblock %} {% block content %} <h1>Resend Account Activation</h1> <p>Enter your email address below, and we'll email t
{% extends 'base.html' %}
{% block title %}Forgot Your Password?{% endblock %}
{% block content %}
<h1>Resend Account Activation</h1>
<p>Enter your email address below, and we'll email the activation link.</p>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Send me instructions!">
</form>
</div>
{% endblock %}
及
及
我做了以下工作:
我创建了一个视图:
def resend_account_activation(request):
if request.method == 'POST':
form = ResendActivationLinkForm(request.POST)
if form.is_valid():
active_users = User._default_manager.filter(**{
'%s__iexact' % User.get_email_field_name(): form.cleaned_data['email'],
'is_active': False,
})
if active_users:
print('****************************')
print(active_users[0])
current_site = get_current_site(request)
subject = 'Activate Your MySite Account'
message = render_to_string('account_activation_email.html', {
'user': active_users[0],
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(active_users[0].pk)).decode(),
'token': account_activation_token.make_token(active_users[0]),
})
active_users[0].email_user(subject, message)
return redirect('account_activation_sent')
else:
return redirect('account_activation_sent')
else:
print("else")
form = ResendActivationLinkForm()
print("ending before render")
return render(request, 'account_activation_resend.html', {'form': form})
还有一张表格
class ResendActivationLinkForm(forms.Form):
email = forms.EmailField(label=_("Email"), max_length=254)
现在我可以发送电子邮件并再次获得激活链接。酷。从不知道用户类的
email\u User
方法。
path('signup/', views.signup, name='signup'),
def resend_account_activation(request):
if request.method == 'POST':
form = ResendActivationLinkForm(request.POST)
if form.is_valid():
active_users = User._default_manager.filter(**{
'%s__iexact' % User.get_email_field_name(): form.cleaned_data['email'],
'is_active': False,
})
if active_users:
print('****************************')
print(active_users[0])
current_site = get_current_site(request)
subject = 'Activate Your MySite Account'
message = render_to_string('account_activation_email.html', {
'user': active_users[0],
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(active_users[0].pk)).decode(),
'token': account_activation_token.make_token(active_users[0]),
})
active_users[0].email_user(subject, message)
return redirect('account_activation_sent')
else:
return redirect('account_activation_sent')
else:
print("else")
form = ResendActivationLinkForm()
print("ending before render")
return render(request, 'account_activation_resend.html', {'form': form})
class ResendActivationLinkForm(forms.Form):
email = forms.EmailField(label=_("Email"), max_length=254)