How can I add uuid to url in urls.py django?

I have django app with authentication in it and email verification. When user is created activation email is sent with link inside of it, when user clicks this link is doesn't take him nowhere.

views.py

class customer_register(CreateView):
    model = User
    form_class = CustomerSignUpForm
    template_name = 'authentication/customer_register.html'
   
    def form_valid(self, form):
        user = form.save()
        user.token = str(uuid.uuid4())
        subject = 'Verify your account | Zane'
        message = f"http://127.0.0.1:8000/accounts/verify/{user.token}/"
        recipient_list = [user.email]
        send_mail(
            subject,
            message,
            'from@example.com',
            ['to@example.com'],
            fail_silently=False,
        )
        return redirect('/')




def activate(request, token):
    try:
        obj = models.User.objects.get(email_token = token)
        obj.signup_confirmation = True
        obj.save()
        return HttpResponse('Your account is verified')

    except Exception as e:
        return HttpResponse('Invalid token')

urls.py

path('verify/<uuid:pk>/', views.activate, name='activate'),

models.py

...
token = models.CharField(max_length=200, blank=True)
signup_confirmation = models.BooleanField(default=False)

I wonder what do I need to put in my url to trigger my function?

I would rewrite your active view as a class. Here is an example:

class ActivateView(LoginRequiredMixin, View):
    def get(self, request, *args, **kwargs):
       token = kwargs['pk']
       try:
           obj = User.objects.get(email_token = token)
           obj.signup_confirmation = True
           obj.save()
           return HttpResponse('Your account is verified')

       except Exception as e:
           return HttpResponse('Invalid token')

urls.py

path('verify/<uuid:pk>/', ActivateView.as_view(), name='activate'),
       
Back to Top