【问题标题】:Profile() got an unexpected keyword argument 'user'Profile() 有一个意外的关键字参数“用户”
【发布时间】:2021-04-26 03:51:01
【问题描述】:

您好,我正在使用 Django。 我正在尝试将我的用户变成带有信号的个人资料

通过表单注册用户时

我收到以下错误: TypeError at /Registro/ Profile() 有一个意外的关键字参数 'user' 和 用户是在 'AUTHENTICATION AND AUTHORIZATION' (ADMIN) 中创建的,但不是在配置文件中。

Models.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"

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

forms.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',
                ]

signals.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)

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

【问题讨论】:

  • 与您的问题并没有真正严格相关,但现在 Django 有 better ways 扩展 user 模型。
  • 感谢您的评论,您知道如何告诉我吗?

标签: python django django-models django-forms django-signals


【解决方案1】:

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

只需将user fk 添加到User 模型并在信号中创建一次即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 2017-09-26
    • 1970-01-01
    • 2016-09-12
    • 2018-04-26
    • 1970-01-01
    相关资源
    最近更新 更多