An activation email is sent and I have logged that it comes back in when the link is selected. However, it gives an error when decoding it.
This code:
uid = force_str(urlsafe_base64_decode(uidb64))
gets this error:
Field ‘id’ expected a number but got b’<property object at 0x000002553FA59DA0>'.
The above exception (invalid literal for int() with base 10: b’<property object at 0x000002553FA59DA0>') was the direct cause of the following exception:
Code:
views.py
current_site = get_current_site(self.request)
subject = 'Activate your account'
message = render_to_string('account_activation_email.html', {
'user': CustomUser,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(CustomUser.pk)),
'token': account_activation_token.make_token(CustomUser),
})
send_mail(subject, message, 'xxxxxxxxxxxxxxxx', [user_email])
def account_activation_sent(request):
return render(request, 'account_activation_sent.html')
def activate(request, uidb64, token):
try:
uid = force_str(urlsafe_base64_decode(uidb64))
user = CustomUser.objects.get(pk=uid)
except (TypeError, ValueError, OverflowError, CustomUser.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('account_activation_complete')
else:
return HttpResponseBadRequest('Activation link is invalid!')
def account_activation_complete(request):
return render(request, 'account_activation_complete.html')
HTML
{% load static %}
{% autoescape off %}
Hello {{ user.username }},
Please click the link below to activate your account:
{% block reset_link %}
<a href="http://{{ domain }}{% url 'activate' uidb64=uid token=token %}">Activate your account"></a>
{% endblock %}
If you did not register on our site, please ignore this message.
Best regards,
Your Website Team
{% endautoescape %}
app/urls
urlpatterns = [
path('signup/<str:alias_name>', views.SignUpView.as_view(), name='signup.html'),
path('activate/<uidb64>/<token>/', views.activate, name='activate'),
path('account_activation_sent/', views.account_activation_sent, name='account_activation_sent'),
path('account_activation_complete/', views.account_activation_complete, name='account_activation_complete'),
]