В Django по книге Дронова при регистрации отправляется письмо активации, но при переходе по ссылке происходит ошибка URL, как быть?
view
def user_activate(request, sign):
try:
username = signer.unsign(sign)
except BadSignature:
return render(request, 'main/bad_signature.html')
user = get_object_or_404(AdvUser, username=username)
if user.is_activated:
return render(request, 'main/user_is_activated.html')
else:
user.is_active = True
user.is_activated = True
user.save()
return render(request, 'main/activation_done.html')
forms
def clean_password1(self):
password1 = self.cleaned_data['password1']
try:
password_validation.validate_password(password1)
except forms.ValidationError as error:
self.add_error('password1', error)
return password1
def clean(self):
super().clean()
password1 = self.cleaned_data['password1']
password2 = self.cleaned_data['password2']
if password1 and password2 and password1 != password2:
errors = {'password2': ValidationError(
'Введенные пароли не совпадают', code='password_mismatch')}
raise ValidationError(errors)
urls
app_name = 'main'
urlpatterns = [
path('accounts/register/activate/<str:sign>/', user_activate, name='register_activate'),
path('accounts/logout/', BBLogoutView.as_view(), name='logout'),
path('accounts/profile/change', ChangeUserInfoView.as_view(), name='profile_change'),
path('', index, name='index'),
path('<str:page>/', other_page, name='other'),
path('accounts/register/done/', RegisterDoneView.as_view(), name='register_done'),
path('accounts/register/', RegisterUserView.as_view(), name='register'),
path('accounts/login/', BBLoginView.as_view(), name='login'),
path('accounts/profile/', profile, name='profile'),
path('accounts/password/change/', BBPasswordChangeView.as_view(), name='password_change'),
]
apps
user_registered = Signal()
def user_registered_dispatcher(sender, **kwargs):
send_activation_notification(kwargs['instance'])
user_registered.connect(user_registered_dispatcher)
utilities
signer = Signer()
def send_activation_notification(user):
if ALLOWED_HOSTS:
host = 'http://' + ALLOWED_HOSTS[0]
else:
host = 'http://localhost:8000'
context = {'user': user, 'host': host, 'sign': signer.sign(user.username)}
subject = render_to_string('email/activation_letter_subject.text', context)
body_text = render_to_string('email/activation_letter_body.text', context)
user.email_user(subject, body_text)