Home > Enterprise >  password1 and password2 doesn't save in django form
password1 and password2 doesn't save in django form

Time:05-04

I have created a (CustomUserForm) like the below:

from django.contrib.auth.forms import UserChangeForm
from .models import User
from django import forms


class CustomUserForm(UserChangeForm):
    username = forms.CharField(
        widget=forms.TextInput(attrs={'class': 'form-control my-2', 'placeholder': 'Enter Username'}))
    email = forms.CharField(
        widget=forms.TextInput(attrs={'class': 'form-control my-2', 'placeholder': 'Enter The Email'}))
    password1 = forms.CharField(
        widget=forms.PasswordInput(attrs={'class': 'form-control my-2', 'placeholder': 'Enter The Password'}))
    password2 = forms.CharField(
        widget=forms.PasswordInput(attrs={'class': 'form-control my-2', 'placeholder': 'Confirm Password'}))

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

and the corresponding view to create a new user:

from django.shortcuts import render, redirect
from django.contrib import messages
from store.forms import CustomUserForm


def register(request, self):
    form = CustomUserForm()
    if request.method == 'POST':
        form = CustomUserForm(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, "Registered Successfully")
            return redirect('/login')
    context = {'form': form}
    return render(request, 'auth/register.html', context)


def login_page(request):
    return render(request, 'auth/login.html')

and this is the register.html

{% extends 'layouts/main.html' %}
{% block title %} {{ category }} {% endblock %}
{% block content %}


    <section  style="background-color: #eee;">
  <div >
    <div >
      <div >
        <div  style="border-radius: 25px;">
          <div >
            <div >
              <div >

                <p >Sign up</p>

                <form  action="" method="POST">
                  {% csrf_token %}
                  <div >
                    <i ></i>
                    <div >
                      <label  for="form3Example1c">Your Name</label>
                        {{ form.username }}
                        {% if form.errors.username %}
                            <span >{{ form.errors.username }}</span>
                        {% endif %}
                    </div>
                  </div>

                  <div >
                    <i ></i>
                    <div >
                      <label  for="form3Example3c">Your Email</label>
                        {{ form.email }}
                        {% if form.errors.email %}
                            <span >{{ form.errors.email }}</span>
                        {% endif %}
                    </div>
                  </div>

                  <div >
                    <i ></i>
                    <div >
                      <label  for="form3Example4c">Password</label>
                        {{ form.password1 }}
                        {% if form.errors.password1 %}
                            <span >{{ form.errors.password1 }}</span>
                        {% endif %}
                    </div>
                  </div>

                  <div >
                    <i ></i>
                    <div >
                      <label  for="form3Example4cd">Repeat your password</label>
                        {{ form.password2 }}
                        {% if form.errors.password2 %}
                            <span >{{ form.errors.password2 }}</span>
                        {% endif %}
                    </div>
                  </div>

                  <div >
                    <input  type="checkbox" value="" id="form2Example3c" />
                    <label  for="form2Example3">
                      I agree all statements in <a href="#!">Terms of service</a>
                    </label>
                  </div>

                  <div >
                    <button type="submit" >Register</button>
                  </div>

                </form>

              </div>
              <div >

                <img src="https://mdbcdn.b-cdn.net/img/Photos/new-templates/bootstrap-registration/draw1.webp"
                   alt="Sample image">

              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>

{% endblock %}

everything are worked well, except in MySQL database there is no password created !!

CodePudding user response:

I think, password needs to be hashed properly, as raw passwords aren't stored by django.

You can make use of cleaned_data,and you need to hash the password properly, so you can use make_password() from from django.contrib.auth.hashers import make_password.

So, with your current code do following modifications in views.py.

from django.contrib.auth.models import User
from django.contrib.auth.hashers import make_password

def register(request, self):
    if request.method == 'POST':
        form = CustomUserForm(request.POST)
        if form.is_valid():
            username=form.cleaned_data['username']
            email=form.cleaned_data['email']
            password=make_password(form.cleaned_data['password1'])
            data=User(username=username,email=email,password=password)
            data.save()            
            messages.success(request, "Registered Successfully")
            return redirect('/login/')
    else: #Here GET condition
        form = CustomUserForm()
    context = {'form': form}
    return render(request, 'auth/register.html', context)

Note: You must give / at the end of every route.

  • Related