没有成功添加我的配置文件模型字段数据到数据库django

问题描述 投票:0回答:1
My models.py file

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(max_length=500, blank=True)
    location = models.CharField(max_length=30, blank=True)
    birth_date = models.DateField(null=True, blank=True)

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

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

My forms.py file
class UserForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('username', 'email', 'password')

class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ('bio', 'location', 'birth_date')

My views.py file
def signup(request):
    if request.method == 'POST':
        user_form = UserForm(request.POST)
        profile_form = ProfileForm(request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            username = user_form.cleaned_data['username']
            email = user_form.cleaned_data['email']
            password = user_form.cleaned_data['password']
            bio = profile_form.cleaned_data['bio']
            location = profile_form.cleaned_data['location']
            user = User.objects.create_user(username,email,password)
            user.save()
            profile = Profile(user=user, bio=bio, location=location)
            profile.save()
            messages.success(request, _('Your profile was successfully updated!'))
            return redirect('login')
   else:
        user_form = UserForm()
        profile_form = ProfileForm()
    return render(request, 'profile.html', {
        'user_form': user_form,
        'profile_form': profile_form
    })
Not successfully added my profile model fields data to database but added id.Please help
  Not successfully added my profile model fields data to database but added id.Please help

未成功将我的资料模型字段数据添加到数据库,但添加了id.please help 未成功将我的资料模型字段数据添加到数据库,但添加了id.please help 未成功将我的资料模型字段数据添加到数据库,但添加了id.please help。

django forms model field extend
1个回答
0
投票

你需要在视图中保存profile表单,因为在创建用户后,信号会自动创建一个profile。你可以在这个视图中工作。

def signup(request):
if request.method == 'POST':
    user_form = UserForm(request.POST)

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

        return redirect('login')
   else:
        user_form = UserForm()

return render(request, 'profile.html', {'user_form': user_form})
© www.soinside.com 2019 - 2024. All rights reserved.