0

I am working on a django project with MySQL. I am working on the signup part for the candidate with an activation link sent in the email. When I connected django to MySQL Workbench, It generated a Model file including the "Candidats form" which I am using in my view.

The problem is that I cannot decode the uidb64 from the URL. I can send a link in the email but it always return "Activation went wrong" and when I debugged my code I found out that my methid is not returing the uid but None.

This is my view.py

from django.urls import reverse
from .tokenssss import token_generator
from django.core.mail import EmailMessage
from django.contrib.sites.shortcuts import get_current_site
from django.shortcuts import render
from django.http import HttpResponse
from django.utils.encoding import force_bytes, force_str
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.views import View
from .models import Candidats, Recruteurs

def sign_up_candidat(request):
    if request.method == "POST":
        nom = request.POST.get('nom')
        prenom = request.POST.get('prenom')
        date = request.POST.get('date')
        sexe = request.POST.get('sexe')
        telephone = request.POST.get('telephone')
        email = request.POST.get('email')
        password = request.POST.get('pass')
        password_c = request.POST.get('pass_c')
        if password != password_c:
            return HttpResponse("Confirmation password went wrong")
        else:
            my_user = Candidats(nomcandidat=nom, prenomcandidat=prenom, datenaissance=date, sexe=sexe,
                                telephonecandidat=telephone, emailcandidat=email, passwordcandidat=password)

            uidb64 = urlsafe_base64_encode(force_bytes(my_user.idcandidat))
            domain = get_current_site(request).domain
            link = reverse('activate',kwargs={'uidb64': uidb64,'token':token_generator.make_token(my_user)})

            activate_url = "http://"+domain+link
            email_body = 'Bonjour '+ my_user.nomcandidat+" "+my_user.prenomcandidat+" \n activation link "+activate_url
            mail = EmailMessage(
                'Confirmation mail',
                email_body,
                'medjahedwassim@yahoo.com',
                [email],
            )
            mail.send(fail_silently=False)
            my_user.save()
            return HttpResponse("Check your mail")
    else:
        return render(request, 'signup_candidat.html')


class VerificationView(View):
    def get(self,request,uidb64,token):
        try:
            uid = force_str(urlsafe_base64_decode(uidb64))
            myuser = Candidats.objects.get(idcandidat=uid)
        except(TypeError, ValueError, OverflowError, Candidats.DoesNotExist):
            myuser = None
        if myuser is not None and token_generator.check_token(myuser, token):
            token_generator.check_token(myuser, token)
            myuser.isactive = True
            myuser.save()
            return HttpResponse("activation done")
        else:
            return HttpResponse('Activation went wrong')

This is the url.py

from django.urls import path
from . import views
from .views import VerificationView

urlpatterns = [
    path('signup/', views.signup,name='signup'),
    path('signup/candidat/', views.sign_up_candidat, name='sign_up_candidat'),
    path('signup/recruteur/', views.sign_up_recruteur, name='sign_up_recruteur'),
    path('activate/<uidb64>/<token>',VerificationView.as_view(),name='activate')
]

This is the token file:

from django.contrib.auth.tokens import PasswordResetTokenGenerator
from six import text_type


class AppTokenGenerator(PasswordResetTokenGenerator):
    def _make_hash_value(self, user, timestamp):
        return (
            text_type(user.isactive) + text_type(user.idcandidat)+text_type(timestamp)
        )

token_generator = AppTokenGenerator()

I know there are things missing in my view but I just wanted to make sure it works and return back to the code later.

I tried a lot of tutorials in Youtube but I am facing the same problem.

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40

0 Answers0