首页 > 解决方案 > Profile() 有一个意外的关键字参数“用户”

问题描述

嗨,我与 Django 合作。我正在尝试将我的用户变成带有信号的个人资料

通过表单注册用户时

我收到以下错误: /Registro/ Profile() 处的 TypeError 获得了意外的关键字参数“用户”,并且用户是在“身份验证和授权”(管理员)中创建的,但不是在配置文件中。

模型.py

from django.db import models


class Profile(models.Model):
    id = models.AutoField(primary_key=True)
    nombreUsuario = models.CharField('Nombre usuario : ',  max_length=15, null = False, blank=False, unique=True)
    email = models.EmailField('Email', null=False, blank=False, unique=True)
    password = models.CharField('Contraseña',  max_length=25, null=False, blank=False, default='')
    #Unique sirve para validar si el usuario existe y sea unico el email y nombre de usuario.
    nombres = models.CharField('Nombres', max_length=255, null= True, blank=True)
    apellidos = models.CharField('Apellidos', max_length=255, null=True, blank=True)
    imagen = models.ImageField(upload_to='img_perfil/',default='batman.png',null=True, blank=True)
    fecha_union = models.DateField('Fecha de alta', auto_now = False, auto_now_add = True)
    facebook = models.URLField('Facebook', null=True, blank=True)
    instagram = models.URLField('Instagram', null=True, blank=True)

    def __str__(self):
        return f'Perfil de {self.nombreUsuario}'

    class Meta:
        verbose_name = "Perfil"
        verbose_name_plural = "Perfiles"

视图.py

from django.shortcuts import render, redirect
from django.http import HttpResponseRedirect
from django.views.generic.edit import FormView
from .models import Profile

from .forms import RegistrationForm
from django.contrib import messages
from django.contrib.auth.models import Group
from django.utils.decorators import method_decorator

def iniciarSesion(request):
    return render(request,'social/inicio.html')

def registro(request):
    if request.method == 'POST':
        fm = RegistrationForm(request.POST)
        if fm.is_valid():
            user=fm.save()
            username = fm.cleaned_data.get('username')

            messages.success(request,'Registration Created Successfully')
            redirect('feed')
    else:
        fm = RegistrationForm()
    return render(request, 'social/registrarse.html',{'fm':fm})

def feed(request):
    return render(request,'social/feed.html')


def profile(request):
    return render(request,'social/profile.html')

表格.py

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


class RegistrationForm(UserCreationForm):

    class Meta:
        model=User 
        fields=[
                'username',
                'email',
                'first_name',
                'last_name',
                ]

信号.py

from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.contrib.auth.models import Group
from .models import Profile



def create_user_profile(sender, instance, created, **kwargs):
    if created:
        #group = Group.objects.get(name = 'profile')
        #instance.groups.add(group)

        Profile.objects.create(
            user = instance,
            name= instance.username,
            )
        Profile.objects.create(user=instance)

post_save.connect(create_user_profile, sender=User)

我需要有关此代码的帮助!

标签: pythondjangodjango-modelsdjango-formsdjango-signals

解决方案


好吧,你到底期待什么?您的Profile模型似乎对用户没有 fk,这就是您在信号中尝试做的(两次)。

只需将userfk 添加到User模型并在信号中创建一次。


推荐阅读