Exemple #1
0
    def post(self, request, *args, **kwargs):
        form = UserRegistrationForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.is_active = False
            user.save()
            message_subject = "Activate your account"
            domain_url = get_current_site(request)
            user_email = form.cleaned_data["email"]
            message = render_to_string(
                "accounts/activation_message.html",
                {
                    "domain": domain_url.domain,
                    "user": user,
                    "uid": urlsafe_base64_encode(force_bytes(user.id)),
                    "token": activation_token.make_token(user),
                },
            )

            email = EmailMessage(message_subject, message, to=[user_email])
            email.send()
            activation_msg = "Open your email to activate account."
            return render(
                request, "accounts/activate_email.html", {"activation_msg": activation_msg}
            )

        template_name = "accounts/signup.html"
        return render(request, template_name, {"form": form})
Exemple #2
0
    def post(self, request, *args, **kwargs):
        form = UserRegistrationForm(request.POST)
        if form.is_valid(
        ):  # the django itself will lookupon the constraints for validation
            user = form.save(commit=False)  # not to save in db directly
            user.is_active = False  # initially it is set to false
            user.save()
            message_subject = "Activate your account"
            domain_url = get_current_site(
                request)  # request garney ko domain_url
            user_email = form.cleaned_data[
                "email"]  # only extracting the clean email
            message = render_to_string(  # whle we send the message it is similar like returning render with other parameters
                "accounts/activation_message.html",
                {
                    "domain": domain_url.domain,
                    "uid": urlsafe_base64_encode(force_bytes(user.id)), #first change to bytes so that comp can read and then encode
                    "token": activation_token.make_token(user),
                },
            )

            email = EmailMessage(message_subject, message, to=[
                user_email
            ])  # here u can add other reqd. fields such as cc, bcc etc.
            email.send()
            activation_msg = "Open your email to activate the account."
            return render(
                request,
                "accounts/activate_email.html",
                {"activation_msg": activation_msg
                 }  # no need for urls since it is a part of activate fn
            )

        template_name = "accounts/signup.html"
        return render(request, template_name, {"form": form})