按照this教程创建注册视图后,我遇到了错误。
以下是观点:
def signup(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
new_user = form.save(commit=False)
new_user.is_active = False
new_user.save()
current_site = get_current_site(request)
subject = "Activate your DC-Elects account"
message = render_to_string('registration/account_activation_email.html', {
'user': new_user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(used.id)),
'token': account_activation_token.make_token(user),
})
new_user.email_user(subject, message)
return reverse('elections:account_activation_sent')
else:
form = SignUpForm()
return render(request, 'registration/signup.html',{
'form': form,
})
以下是扩展用户模型:
class Profile(models.Model):
UserID = models.OneToOneField(User, on_delete=models.CASCADE)
GENDERS = (("M","Male"), ("F","Female"))
Gender = models.CharField(max_length=1, choices=GENDERS)
UserTypeID = models.ForeignKey(UserType, on_delete=models.PROTECT, blank=True, null=True)
ProfilePicture = models.ImageField(default='default_profile.png', upload_to="imageposts/")
EmailConfirmed = models.BooleanField(default=False)
# For student number, can you have choices - Teacher or input()?
@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()
以用户身份填写表单时一切正常,但是当我提交表单时,我收到此错误:
Traceback (most recent call last):
File "C:\python\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "C:\python\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\python\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\cbuch1800\Coursework\mysite\elections\views.py", line 325, in signup
new_user.save()
File "C:\python\lib\site-packages\django\contrib\auth\base_user.py", line 80, in save
super(AbstractBaseUser, self).save(*args, **kwargs)
File "C:\python\lib\site-packages\django\db\models\base.py", line 808, in save
force_update=force_update, update_fields=update_fields)
File "C:\python\lib\site-packages\django\db\models\base.py", line 848, in save_base
update_fields=update_fields, raw=raw, using=using,
File "C:\python\lib\site-packages\django\dispatch\dispatcher.py", line 193, in send
for receiver in self._live_receivers(sender)
File "C:\python\lib\site-packages\django\dispatch\dispatcher.py", line 193, in <listcomp>
for receiver in self._live_receivers(sender)
File "C:\Users\cbuch1800\Coursework\mysite\elections\models.py", line 25, in create_user_profile
Profile.objects.create(user=instance)
File "C:\python\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\python\lib\site-packages\django\db\models\query.py", line 392, in create
obj = self.model(**kwargs)
File "C:\python\lib\site-packages\django\db\models\base.py", line 573, in __init__
raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
TypeError: 'user' is an invalid keyword argument for this function
我不确定引发错误的位置或原因,因为新用户仍在创建并保存到数据库中,我只是收到此错误而不是发送的电子邮件。
如何防止此错误?
答案 0 :(得分:1)
您在个人资料模型中有字段UserID
,因此您需要在创建方法而不是user
中使用它:
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(UserID=instance)
答案 1 :(得分:0)
Profile.objects.create(user=instance)
您的Profile
模型定义不包含任何名为user
的字段。