python - 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)
我需要有关此代码的帮助!
解决方案
好吧,你到底期待什么?您的Profile
模型似乎对用户没有 fk,这就是您在信号中尝试做的(两次)。
只需将user
fk 添加到User
模型并在信号中创建一次。
推荐阅读
- button - 当我单击按钮时,出现错误:尝试在空实例的基础“空实例”中调用函数“显示”
- python - 如何使用 /tmp/ 读取 App Engine 上的文件?我得到 [Errno 2] 没有这样的文件或目录
- postgresql - 将主机名转换为地址时,pgAdmin 外部数据包装器错误
- ios - Swift 5 使用属性文本向 UILabel 添加 alpha
- firebase - Firebase 托管标头 firebase.json 不起作用
- c# - Xamarin.Forms 中的模块
- sql - 选择 Distinct Records 并计算与记录相关的其他列
- python - 计算列表最后一天和今天之间的天数
- pip - 通过 sh 脚本自动创建 pyenv
- reactjs - 类型错误:path_to_regexp__WEBPACK_IMPORTED_MODULE_8___default.a.compile 不是函数