使用电子邮件注册,Django 2.0 [英] Registration using e-mail, Django 2.0
问题描述
我只是一个初学者,所以在制作第一个项目时遇到了一些问题。我在视图中有代码:
I'm just a beginner, so i got some questions making my first project. I've got code in views:
def signup(request):
if request.method == 'POST':
form = SignupForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.save()
current_site = get_current_site(request)
mail_subject = 'Активация'
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),
})
print(message) # здесь я смотрю какое сообщение отправляю
to_email = form.cleaned_data.get('email')
email = EmailMessage(
mail_subject, message, to=[to_email]
)
email.send()
return HttpResponse('Пожалуйста, подтвердите адрес электронной почты')
else:
form = SignupForm()
return render(request, 'signup.html', {'form': form})
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_active = 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!')
此代码来自网址:
from . import views
from django.urls import path
urlpatterns = [
path('', views.signup, name='signup'),
path('activate/?P<uidb64>[0-9A-Za-z_\-]+/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
views.activate, name='activate'),
]
问题是我总是在电子邮件中收到无效的URL。
我认为这与新的路径功能有关,可以使用的是
The problem is that i always get invalid URL in my email. I think it is about new 'path' function, which may be used is
<int:uidb64>
但不是很确定。
感谢您的帮助!
but not really sure. Thank for your help!
推荐答案
您不能使用 [0- 9A-Za-z_://-] +
,当您使用 path()
。如果要使用正则表达式,请使用 re_path
(与旧版Django中的 url()
相同)。
You can't use regexes like [0-9A-Za-z_\-]+
when you use path()
. If you want to use regexes, then use re_path
(which works the same as url()
from older versions of Django).
使用 path()
时,可以使用内置的路径转换器。您不能使用< int:uidb64>
,因为uidb可以包含 A-Za-z
,连字符,和下划线,而不仅仅是数字。
When you use path()
, you can use one of the built-in path converters. You can't use <int:uidb64>
, because uidb can contain A-Za-z
, hyphens, and underscores, not just digits.
对于您的 uidb64
和令牌
,我认为 slug
是Django中最适合的路径转换器。
For your uidb64
and token
, I think slug
is the most suitable of the path converters included in Django.
path('activate/<slug:uidb64>/<slug:token>/', views.activate, name='activate'),
这将匹配您的正则表达式不允许的子段和令牌,但是只要您 check_token
方法为这些无效值返回 False
,并且不会引发错误。
This will match slugs and tokens which your regex wouldn't allow, but this isn't a problem as long as your check_token
method returns False
for these invalid values and does not raise an error.
这篇关于使用电子邮件注册,Django 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!