Home > front end >  Some objects of django forms are not saved in database?
Some objects of django forms are not saved in database?

Time:12-06

I created a custom register form and previously it only asked for username and password.Then I added some sections such as name , lastname and email to the user registration section. After adding these, it now doesn't happen when registering the username in the database during user registration. And also name and email information is not registered.

models.py

from django.db import models

class Register(models.Model):
    first_name = models.CharField(max_length=50,verbose_name="First Name")
    last_name = models.CharField(max_length=50,verbose_name="Last Name")
    username = models.CharField(max_length=50,verbose_name="Username")
    email = models.EmailField(verbose_name="Email")
    password = models.CharField(max_length=50,verbose_name="Password")
    confirm = models.CharField(max_length=50,verbose_name="Confirm Password")

    def __str__(self):
        return self.title

forms.py

   from django import forms
   from.models import Register

class LoginForm(forms.Form):
    username = forms.CharField(label="Username")
    password = forms.CharField(label="Password",widget=forms.PasswordInput)


class RegisterForm(forms.ModelForm):
    class Meta:
        model = Register
        widgets = {
        'password': forms.PasswordInput(),
        'confirm': forms.PasswordInput(),

    }
        fields = ["first_name","last_name","username","email","password","confirm"]

views.py

from django.contrib.auth.backends import UserModel
from django.shortcuts import render,redirect
from .forms import LoginForm, RegisterForm
from django.contrib.auth.models import User
from django.contrib.auth import login,authenticate,logout
from django.contrib import messages
from django.contrib.auth.decorators import login_required

# Create your views here.


def register(request):
    
    form = RegisterForm(request.POST or None)
    if form.is_valid():
            
            password = form.cleaned_data.get('password')
            username = form.cleaned_data.get('username')
            last_name = form.cleaned_data.get('last_name')
            email = form.cleaned_data.get('email')
            first_name = form.cleaned_data.get('first_name')
            
            
            newUser = User(username = username)
            newUser = User(first_name = first_name)
            newUser = User(email = email)
            newUser = User(last_name = last_name)
            newUser.set_password(password)

            newUser.save()

            login(request,newUser)
            messages.success(request,"Register successfully!")

            return redirect("index")
        
    context = {
        "form" : form
        }
    return render(request,"register.html",context)


def loginUser(request):
    form = LoginForm(request.POST or None)
    context = {
        "form" : form
    }

    if form.is_valid():
        username = form.cleaned_data.get('username')
        password = form.cleaned_data.get('password')
        
        user = authenticate(username = username, password = password)

        if user is None : 
            messages.info(request,"Username or password is wrong!")
            return render (request,"login.html",context)
        
        messages.success(request,"Login Successfully")
        login(request,user)
        return redirect("index")
    
    return render(request,"login.html",context)

def logoutUser(request):
    logout(request)
    messages.success(request,"Successfully logged out")
    return redirect("index")

@login_required(login_url="user:login")
def profileUser(request):
   return render(request,"profile.html")

My Registration Form is Like : Registration form picture :

Just register : During register picture

After Registration there is no username , email , fistname in database. Only password and lastname information in database.

After Registration Database is looking like picture: Yellow lines is empty areas..

CodePudding user response:

try this

def register(request):
    
    form = RegisterForm(request.POST or None)
    if form.is_valid():
            
            password = form.cleaned_data.get('password')
            username = form.cleaned_data.get('username')
            last_name = form.cleaned_data.get('last_name')
            email = form.cleaned_data.get('email')
            first_name = form.cleaned_data.get('first_name')
            
            
            newUser = User(username = username,last_name = last_name,first_name = first_name,email = email)
            newUser.set_password(password)

            newUser.save()

            login(request,newUser)
            messages.success(request,"Register successfully!")

            return redirect("index")
        
    context = {
        "form" : form
        }
    return render(request,"register.html",context)
  • Related