2

views.py

from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm


def register(request):
    if request.method == 'POST':
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            messages.success(request, f'Your account has been created! You are now able to log in')
            return redirect('login')
    else:
        form = UserRegisterForm()
    return render(request, 'users/register.html', {'form': form})

forms.py

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from .models import Profile


class UserRegisterForm(UserCreationForm):
    email = forms.EmailField()

    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2']

using RegisterForm for Registration form django automatically activate user.I want to add signup email confirmation before activating user.

sanjeet pal
  • 139
  • 2
  • 11
  • is the `UserRegisterForm` a model form to User model? can you post that in the question as well. – Sammy J Sep 11 '19 at 02:50
  • please check the answer below, if it solves the issue in your question please do accept it else you can use the comments to ask for clarifications :) – Sammy J Sep 11 '19 at 04:28

1 Answers1

2

So it is pretty simple, you can give a commit=False while saving the form

        if form.is_valid():
            user=form.save(commit=False)
            # sets the field to False
            user.is_active=False
            user.save()
            username = form.cleaned_data.get('username')
            messages.success(request, f'Your account has been created! You are now able to log in')
            return redirect('login')

you have also said that

I want to add signup email confirmation before activating user.

for this, you cancheck out these links from

2017: https://medium.com/@frfahim/django-registration-with-confirmation-email-bb5da011e4ef

or 2018:Django 2 - How to register a user using email confirmation and CBVs?

Sammy J
  • 1,048
  • 10
  • 28