Notifications
Clear all
Mastering Advanced E-Commerce Development with Python Django
0 Posts
1 Users
0 Reactions
3 Views
0
20/01/2025 3:24 am
Topic starter Reference Section 13:
Account Activation – Decode User PK & Activate the User | Expire Link
I am not receiving an account verification email.
```
# verification email
from django.contrib.sites.shortcuts import get_current_site
from django.template.loader import render_to_string
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.utils.encoding import force_bytes
from django.contrib.auth.tokens import default_token_generator
from django.core.mail import EmailMessage
views.py
# user activation
current_site = get_current_site(request)
mail_subject = 'Please activate your account'
message = render_to_string('accounts/account_verification_email.html', {
'user': user,
'domain': current_site,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': default_token_generator.make_token(user),
})
to_email = email
send_email = EmailMessage(mail_subject, message, to=[to_email])
messages.success(request, 'Registration successful')
return redirect('register')
else:
form = RegistrationForm()
context = {
'form':form,
}
return render(request, 'accounts/register.html', context)
def activate(request, uidb64, token):
return HttpResponse('Okay')
```
urls.py
```
path('activate/<uidb64>/<token>/', views.activate, name='activate'),
```
account_verification_mail.html
```
{% autoescape off %}
Hi {{ user.user_name }},
Please click on below link to confirm your registration.
http://{{ domain }}{% url 'activate' uidb64=uid token=token %}
If you think it's not you, please ignore this email.
{% endautoescape %}
```