Django 2 注册确认电子邮件指向主页而不是登录页面
Django 2 register confirmation email directs to homepage instead of login page
我正在设置我的 Django 2.1 应用程序,这样当用户注册时,他们首先会收到一封确认电子邮件,其中包含 link 单击以激活帐户。
到目前为止,我已经成功设置了它(在另一个 post 的帮助下)并且一切正常,直到单击激活电子邮件为止。
当我将 link 复制并粘贴到浏览器中时,它会将我定向到主登录页面,而不是登录页面。
有没有人可以看看我的观点,看看是否有遗漏的地方?
def register_view(request):
if request.method == 'POST':
# Post request.
form = UserRegisterForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.save()
mail_subject = 'Activate your account.'
current_site = get_current_site(request)
uid = urlsafe_base64_encode(force_bytes(user.pk)).decode()
token = account_activation_token.make_token(user)
activation_link = "{0}/?uid={1}&token{2}".format(current_site, uid, token)
message = "Hello {0},\n {1}".format(user.username, activation_link)
to_email = 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:
# Get request.
form = UserRegisterForm()
return render(request, 'users/register.html', {'form': form})
def activate(request, uidb64, token):
try:
uid = urlsafe_base64_decode(uidb64).decode()
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):
# activate user and login:
user.is_active = True
user.save()
messages.success(request, f'Your account has been created {username}. Login.')
return render(request, 'users/login.html')
else:
return HttpResponse('Activation link is invalid!')
urls.py
path('activate/<str:uid>/<str:token>',
user_views.activate,
name='activate'),
tokens.py
class AccountActivationTokenGenerator(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 = AccountActivationTokenGenerator()
我无法确认整个注册功能是否有效,但为了回答您的具体问题,您的代码正在创建一个 activation_link,它设置为:
activation_link = "{0}/?uid={1}&token{2}".format(current_site, uid, token)
如果你仔细观察,它正在创建类似于:
http://example.com/?uid=....&token=....
但是你在 urls.py 中的路径是
path('activate/<str:uid>/<str:token>', user_views.activate, name='activate')
这意味着它期待类似
的东西
http://example.com/activate/..../....
因此,解决方法就是将 activation_link 更改为
activation_link = "{0}/activate/{1}/{2}".format(current_site, uid, token)
再说一次,只是问你要问的具体问题。
撇开你的问题不谈,你可能想查看现有的软件包,如 django-registration 或更完整的软件包,如 django-allauth(如果以后需要,它支持注册流程和社交登录)
我正在设置我的 Django 2.1 应用程序,这样当用户注册时,他们首先会收到一封确认电子邮件,其中包含 link 单击以激活帐户。
到目前为止,我已经成功设置了它(在另一个 post 的帮助下)并且一切正常,直到单击激活电子邮件为止。
当我将 link 复制并粘贴到浏览器中时,它会将我定向到主登录页面,而不是登录页面。
有没有人可以看看我的观点,看看是否有遗漏的地方?
def register_view(request):
if request.method == 'POST':
# Post request.
form = UserRegisterForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.save()
mail_subject = 'Activate your account.'
current_site = get_current_site(request)
uid = urlsafe_base64_encode(force_bytes(user.pk)).decode()
token = account_activation_token.make_token(user)
activation_link = "{0}/?uid={1}&token{2}".format(current_site, uid, token)
message = "Hello {0},\n {1}".format(user.username, activation_link)
to_email = 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:
# Get request.
form = UserRegisterForm()
return render(request, 'users/register.html', {'form': form})
def activate(request, uidb64, token):
try:
uid = urlsafe_base64_decode(uidb64).decode()
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):
# activate user and login:
user.is_active = True
user.save()
messages.success(request, f'Your account has been created {username}. Login.')
return render(request, 'users/login.html')
else:
return HttpResponse('Activation link is invalid!')
urls.py
path('activate/<str:uid>/<str:token>',
user_views.activate,
name='activate'),
tokens.py
class AccountActivationTokenGenerator(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 = AccountActivationTokenGenerator()
我无法确认整个注册功能是否有效,但为了回答您的具体问题,您的代码正在创建一个 activation_link,它设置为:
activation_link = "{0}/?uid={1}&token{2}".format(current_site, uid, token)
如果你仔细观察,它正在创建类似于:
http://example.com/?uid=....&token=....
但是你在 urls.py 中的路径是
path('activate/<str:uid>/<str:token>', user_views.activate, name='activate')
这意味着它期待类似
的东西http://example.com/activate/..../....
因此,解决方法就是将 activation_link 更改为
activation_link = "{0}/activate/{1}/{2}".format(current_site, uid, token)
再说一次,只是问你要问的具体问题。
撇开你的问题不谈,你可能想查看现有的软件包,如 django-registration 或更完整的软件包,如 django-allauth(如果以后需要,它支持注册流程和社交登录)