电子邮件确认链接在Django中失败

时间:2018-07-24 12:50:49

标签: python django

我正在尝试使用Django使用电子邮件链接和令牌创建确认系统

这是我的文件:

tokens.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() 

这就是我创建令牌的地方

Forms.py

class SignUpForm(UserCreationForm):
    email = forms.EmailField(required=True, help_text='Must be a valid E-mail')
    user_name = forms.CharField(required=True, help_text='')
    password1 = forms.CharField(widget=forms.PasswordInput,
                                help_text='')
    password2 = forms.CharField(widget=forms.PasswordInput,
                                help_text='Password confirmation')
    avatar = forms.ImageField(required=False)

    class Meta:
        model = User
        fields = ('email', 'user_name', 'password1', 'password2', 'avatar')
        labels = {
            'user_name': 'user_name',
            'email': ' email',
            'password1': 'password1',
            'password2': 'password2',
            'avatar': 'avatar',
        }

    def clean_email(self):
        data = self.cleaned_data['email']
        return data.lower()

这就是我创建注册表单的地方

views.py

def signup(request):
    current_user_ip = get_client_ip(request)
    current_temp = Temp.objects.filter(created_by_ip=current_user_ip)
    current_temp.delete()
    if request.method == 'POST':
        signup_form = SignUpForm(request.POST, request.FILES)
        if signup_form.is_valid():
            user = signup_form.save(commit=False)
            user.is_confirmed = False
            user.save()
            current_site = get_current_site(request)
            mail_subject = 'Activate your Jehlum account.'
            message = render_to_string('acc_active_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'token': account_activation_token.make_token(user),
            })
            to_email = signup_form.cleaned_data.get('email')
            email = EmailMessage(
                mail_subject, message, to=[to_email]
            )
            email.send()
            return HttpResponse('Please confirm your email address to complete the registration')
    else:
        signup_form = SignUpForm()

    context = {
        'signup_form': signup_form,
    }
    return render(request, 'signup.html', context)


def activate(request, uidb64, token):
    try:
        uid = force_text(urlsafe_base64_decode(uidb64))
        user = User.objects.get(pk=uid)
    except(TypeError, ValueError, OverflowError, User.DoesNotExist):
        user = None
    if user is not None and account_activation_token.check_token(user, token):
        user.is_confirmed = True
        user.save()
        login(request, user)
        # return redirect('home')
        return HttpResponse('Thank you for your email confirmation. Now you can login your account.')
    else:
        return HttpResponse('Activation link is invalid!')

在这里我创建了注册和激活视图的视图

acc_active_email.html

{% autoescape off %}
Hi {{ user.username }},
Please click on the link to confirm your registration,
http://{{ domain }}{% url 'activate' uidb64=uid token=token %}
{% endautoescape %}

这是正在发送的电子邮件的模板。

最后是我的 urls.py

path('signup/', web_site_views.signup, name='signup'),
path('activate/<uidb64>/<token>/', web_site_views.activate, name='activate'),

每次我尝试使用它时,都会显示(激活链接无效)。因此,这些步骤可以正常进行,并且我可以通过链接向电子邮件发送建议,但是当我单击链接时,它会为我显示链接错误!

0 个答案:

没有答案