I wrote a method to verify the user via email, but it gives a strange error.
AttributeError at /activate/(?PMjM[0-9A-Za-z_-]+)/(?Pc9fdbx-b32d6ca3854bb6e7dd1423fbcc542fec[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$
Manager isn’t available; AbstractUser is abstract
user = User.objects.get(pk=x)
views ^^^^^^^^^^^^
The activate function takes three parameters: request, uidb64, and token. It attempts to decode uidb64 from a URL-safe Base64 string to a readable string using force_str and then converts it to an integer (x). It prints both uidb64 and token for debugging purposes. It retrieves a User object from the database using the x value as the primary key (pk). If the user exists and the token is valid using account_activation_token.check_token, it sets the user’s is_active status to True, saves the user, logs in the user, and returns a confirmation message. If either the user doesn’t exist or the token is invalid, it returns an error message indicating that the activation link is invalid.
#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 = 'Activate your blog account.'
message = render_to_string('registration/acc_active_email.html', {
'user': user,
'domain': current_site.domain,
'uid': force_str(urlsafe_base64_encode(force_bytes(user.pk))),
'token': account_activation_token.make_token(user),
})
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:
form = SignupForm()
return render(request, 'registration/signup.html', {'form': form})
def activate(request, uidb64, token):
try:
print(f"uidb64: {uidb64}, token: {token}")
uid = force_str(urlsafe_base64_decode(uidb64))
x = int(uid)
print(x)
user = User.objects.get(pk=x)
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!')
#token
from django.contrib.auth.tokens import PasswordResetTokenGenerator
import six
class TokenGenerator(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 = Toke
#urls
from django.contrib import admin
from django.conf.urls.static import static
from django.urls import path, include, re_path
from account.views import Login, activate, signup
from . import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('ox.urls')),
path('', include('django.contrib.auth.urls')),
path("login/", Login.as_view(), name="login"),
path('signup/', 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})/$', activate, name='activate'),
path('account/', include('account.urls')),
]
#email
{% autoescape off %}
Hi {{ user.username }},
Please click on the link to confirm your registration,
http://{{ domain }}{% url 'activate' uidb64=uid token=token %}
{% endautoescape %}
aryan ahmadi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.