注册表格未使用扩展的Django用户模型将数据保存在配置文件模型中 [英] Registration Form not saving data in Profile Model, using extended Django User model

查看:46
本文介绍了注册表格未使用扩展的Django用户模型将数据保存在配置文件模型中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 models.py ,在其中与默认的用户" 模型建立 OneToOne 关系.

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

我正在尝试在此处制作 ProfileForm 表单和 InfoProfileForm 表单,以获取 forms.py 中的表单数据.

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')

这是我的 Views.py ,我要在其中合并两个模型(默认的User模型和Profile模型)

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,})

这是我的HTML模板

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,})

这篇关于注册表格未使用扩展的Django用户模型将数据保存在配置文件模型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆