简体   繁体   中英

Registration Form not saving data in Profile Model, using extended Django User model

Recently i am facing a problem in registration. I have made a ** accounts** app in my project for registrations. I don't want to save data in default **User** model in dB. So I have extended **User** model with **OneToOne** relationship with **Profile** model and trying to save the data into "Profile" model but when I submitting the form I don't get any error but data don't save in "Profile" DB.

here is my models.py where I make OneToOne Relationship with default "User" model.

 from django.db import models from django.contrib.auth.models import User # Create your models here. class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) phone=models.CharField(max_length=20) address=models.CharField(max_length=150) def __str__(self): return self.user.username

I am trying to make ProfileForm form and InfoProfileForm form here to get the form data in forms.py .

 from django import forms from django.contrib.auth.models import User from .models import Profile from django.contrib.auth.forms import ReadOnlyPasswordHashField class ProfileForm(forms.ModelForm): password = forms.CharField(max_length=20, widget=forms.PasswordInput) #form = ProfileForm(data=request.POST, user=request.user) class Meta: model = User fields=('username','email','password') #extra info class InfoProfileForm(forms.ModelForm): class Meta: model = Profile fields = ('phone','address')

Here is my Views.py where I am trying to merge two models (default User model and Profile model)

 from django.shortcuts import render from django.http import HttpResponse, JsonResponse # Create your views here. from .forms import ProfileForm,InfoProfileForm from django.http.response import HttpResponse def registration(request): if request.method == 'POST': profile_form = ProfileForm(request.POST) info_form = InfoProfileForm(request.POST) if profile_form.is_valid() and info_form.is_valid(): user = profile_form.save(commit=True) user.set_password(user.password) user.save() profile=info_form.save(commit=False) profile.user = user profile.phone = user.cleaned_data["phone"] profile.address = user.cleaned_data["address"] profile.save() print('submittef') else: HttpResponse("<h1>something wrong</h1>") else: profile_form = ProfileForm(request.POST) info_form = InfoProfileForm(request.POST) return render(request, 'accounts/registration.html',{'profile_form': profile_form,'info_form': info_form,})

here is my HTML template

<form action="{% url 'registration' %}" method="POST"> {% csrf_token %} <div class="container"> <h1>Register</h1> <p>Please fill in this form to create an account.</p> <hr> <label for="name"><b>Name</b></label> <input type="text" placeholder="Enter Name" name="user" required> <label for="email"><b>Email</b></label> <input type="text" placeholder="Enter Email" name="email" required> <label for="phone"><b>Phone</b></label> <input type="text" placeholder="Enter Phone" name="phone" required> <label for="address"><b>Address</b></label> <input type="text" placeholder="Enter Phone" name="address" required> <label for="password"><b>Password</b></label> <input type="password" placeholder="Enter Password" name="password"required> <label for="password2"><b>Repeat Password</b></label> <input type="password" placeholder="Repeat Password" name="password2" required> <hr> <button type="submit" class="registerbtn">Register</button> </div> </form>

May be try referencing the profile model from the user since it's a one to one field

from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
# Create your views here.
from .forms import ProfileForm,InfoProfileForm
from django.http.response import HttpResponse

def registration(request):
    if request.method == 'POST':
        profile_form = ProfileForm(request.POST)
        info_form = InfoProfileForm(request.POST)

        if profile_form.is_valid() and info_form.is_valid():
            user = profile_form.save(commit=True)
            user.set_password(user.password)
            user.profile.phone = user.cleaned_data["phone"]
            user.profile.address = user.cleaned_data["address"]
            user.save()

            print('submitted')
        else:
            HttpResponse("<h1>something wrong</h1>")
    else:
        profile_form = ProfileForm(request.POST)
        info_form = InfoProfileForm(request.POST)
return render(request, 'accounts/registration.html',{'profile_form':profile_form,'info_form': info_form,})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM