重新发送帐户激活电子邮件

时间:2019-11-16 23:33:30

标签: python django

所以我创建了一个模板,用户可以在我的网站上注册。填写注册表格后,激活电子邮件将发送到用户的电子邮件地址。

如果用户没有收到电子邮件,我想重新发送激活电子邮件。我该怎么做?

这是我的代码:

views.py

from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.contrib.auth import login
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.sites.shortcuts import get_current_site
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.template.loader import render_to_string
from django.contrib.auth.models import User
from django.core.mail import EmailMessage
from django.conf import settings

from .forms import UserSignUpForm
from .token_generator import account_activation_token


def signup_view(request):
    if request.method == 'POST':
        form = UserSignUpForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.is_active = False
            user.save()
            current_site = get_current_site(request)
            email_subject = 'Activate Your Account'
            message = render_to_string('accounts/email_templates/account_activation.html', {
                'user': user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'token': account_activation_token.make_token(user),
            })
            to_email = form.cleaned_data.get('email')
            email = EmailMessage(email_subject, message, to=[to_email])
            email.send()
            # TODO: resend activation email
            return HttpResponse('We have sent you an email, please confirm your email address to complete registration')
    else:
        form = UserSignUpForm()
    return render(request, 'accounts/signup.html', {'form': form, 'project_name': settings.PROJECT_NAME})

forms.py

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User


class UserSignUpForm(UserCreationForm):
    email = forms.EmailField(max_length=100, help_text='Required')

    class Meta:
        model = User
        fields = ('username', 'email', 'password1', 'password2')

signup.html

{% extends "base.html" %}
{% load i18n %}
{% block title %}{{ project_name }}{% trans " Registration" %}{% endblock %}

{% block content %}
    <h1>{% trans "Create account" %}</h1>

    <form action="{% url 'accounts:signup' %}" method="post">
        {% csrf_token %}
        {{ form }}
        <input type="submit" value="{% trans 'Create your ' %}{{ project_name }}{% trans ' account' %}"/>
    </form>
{% endblock %}

token_generator.py

from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.utils import six


class TokenGenerator(PasswordResetTokenGenerator):
    def _make_hash_value(self, user, timestamp):
        return (
            six.text_type(user.pk) + six.text_type(timestamp) + six.text_type(user.is_active)
        )


account_activation_token = TokenGenerator()

0 个答案:

没有答案