Django:重新发送激活链接视图

时间:2018-03-26 13:51:53

标签: 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 the activation link.</p>

  <form method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Send me instructions!">
  </form>
</div>
{% endblock %}

我已将内置帐户/ password_reset /用于password_reset。

如何为重新发送激活电子邮件创建视图。 HOw可以使用password_reset的类似代码并用于resend_activation email

我有以下代码,用于在第一次有人报名时发送激活链接。

def signup(request):
    if request.method == 'POST':
        print("inside post")
        form = MyUserCreationForm(request.POST)
        if form.is_valid():
            print("form.is_valid()")
            user = form.save(commit=False)
            user.is_active = False
            user.save()
            current_site = get_current_site(request)
            subject = 'Activate Your MySite Account'
            message = render_to_string('account_activation_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
                'token': account_activation_token.make_token(user),
            })
            user.email_user(subject, message)
            return redirect('account_activation_sent')
    else:
        print("else")
        form = MyUserCreationForm()
    print("ending before render")
    return render(request, 'signup.html', {'form': form})

class MyUserCreationForm(UserCreationForm):
    class Meta(UserCreationForm.Meta):
        model = User
        fields = ("email",)

path('signup/', views.signup, name='signup'),

1 个答案:

答案 0 :(得分:0)

我做了以下事情:

我创建了一个视图:

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)

现在我可以再次发送电子邮件并获取激活链接。