'Form not posting data to database in django

I have a django model form to register a new user into Users table in Django. I have selected fields which I want to use. The form is showing correctly on the html page but the problem is when I submit the form, the data is not saved in the database. I tried to get the field values using name = request.POST['field'] and use print(name) all the data is being printed on the console. Below is my code

forms.py

class UserForm(UserCreationForm):
    first_name = forms.CharField(required=False, label='First Name',
                                 widget=forms.TextInput(
                                     attrs={'style': 'font-size:medium', 'class': 'form-control form-control-sm'}))
    last_name = forms.CharField(required=False, label='Last Name',
                                widget=forms.TextInput(
                                    attrs={'style': 'font-size:medium', 'class': 'form-control form-control-sm'}))
    email = forms.EmailField(required=False, label='Email Address',
                             widget=forms.TextInput(
                                 attrs={'style': 'font-size:medium', 'class': 'form-control form-control-sm'}))
    username = forms.EmailField(required=False, label='Username',
                                widget=forms.TextInput(
                                    attrs={'style': 'font-size:medium', 'class': 'form-control form-control-sm'}))
    password = forms.EmailField(required=False, label='Password',
                                 widget=forms.PasswordInput(
                                     attrs={'style': 'font-size:medium', 'class': 'form-control form-control-sm'}))
    password1 = forms.EmailField(required=False, label='Confirm password',
                                 widget=forms.PasswordInput(
                                     attrs={'style': 'font-size:medium', 'class': 'form-control form-control-sm'}))

    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'email', 'username', 'password','password1')

My UserForm(): class is implementing UserCreationForm in the forms.py class UserForm(UserCreationForm): .

views.py

def reg(request):
   
    if request.method=='POST':
        
        form= UserForm(request.POST)
        if form.is_valid():
            user = form.save()
            group = Group.objects.get(name="patient")
            user.groups.add(group)
            return redirect('profile')
        
    else:
        
        form=UserForm()
    head = 'Create user account'
       
    title = 'Consultation | create account'
    context={'form':form, 'head':head, 'title':title}
    return render(request, 'registration.html', context)

template Registration.html

{% extends 'base-reg.html' %}
{% block content %}
<div class="container-fluid" style="display:flex; align-items: center; justify-content: center;">
    {% for message in messages %}
    {{message}}
    {% endfor %}
    <form action="" method="post" class="card col-md-6 col-sm-12 shadow-sm" style="margin-top: 50px; padding-top: 0px;">
        <div style="background: darkslategray; padding: 10px;" class="shadow">
            <h4 class="text-white" style="width: fit-content; margin: auto;">{{head}}</h4>
        </div>
        <div class="p-3">

            {% csrf_token %}
            <div class="form-group p-2">
                <div class="row" >
                    <div class="col-6">
                        {{ form.first_name.label}}
                         {{ form.first_name}}
                    </div>
                    <div class="col-6">
                        {{ form.last_name.label}}
                        {{ form.last_name}}
                    </div>
                </div>
            </div>
           

            <div class="form-group p-2">
               
                    
                {{ form.email.label}}
                {{ form.email}}
           </div>
           <div class="form-group p-2">
            {{ form.username.label}}
            {{ form.username}}
           </div>
           <div class="form-group p-2">
            <div class="row" >
                <div class="col-6">
                    {{ form.password.label}}
                     {{ form.password}}
                </div>
                <div class="col-6">
                    {{ form.password1.label}}
                    {{ form.password1}}
                </div>
            </div>
           </div>
           
            <div class="form-group p-2">
                <p class="p-2">Already have an account?  <a href="{% url 'login' %}">login here</a></p>
                <input type="submit" value="Create account" class="btn btn-outline-info btn-sm">
            </div>
            
        </div>


    </form>
</div>
{% endblock %}


Solution 1:[1]

I think the problem is where you save in the views.py. Try this:

user = form.save(commit=False)         
group = Group.objects.get(name="patient")                                                                                                                    
user.groups.add(group)
user.save()         
return redirect('profile')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 evanstjabadi