英文:
Django registration with confirmation email, error: save() got an unexpected keyword argument 'commit'
问题
我正在尝试创建确认电子邮件系统,但我得到了与标题中相同的错误,有人知道如何解决吗?
class StudentSignUpView(CreateView):
model = User
form_class = StudentSignUpForm
template_name = 'registration/signup_form.html'
def form_valid(self, form, **kwargs):
user = form.save(commit=False)
user.is_active = False
user.save()
current_site = get_current_site(self.request)
mail_subject = 'Activate your account.'
message = render_to_string('core/acc_active_email.html', {
'user': user,'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': account_activation_token.make_token(user),
})
to_email = form.cleaned_data.get('email')
email = EmailMessage(
mail_subject, message, to=[to_email]
)
email.send()
return HttpResponse('Please confirm your email address to complete the registration')
forms.py
class Meta(UserCreationForm.Meta):
model = User
fields = ['username', 'email', 'password1', 'password2']
@transaction.atomic
def save(self):
user = super().save(commit=False)
user.is_student = True
user.save()
return user
英文:
I'm trying to create confirm email system, bu I got the same error as in title, anyone know how to solve it?
class StudentSignUpView(CreateView):
model = User
form_class = StudentSignUpForm
template_name = 'registration/signup_form.html'
def form_valid(self, form, **kwargs):
user = form.save(commit=False)
user.is_active = False
user.save()
current_site = get_current_site(self.request)
mail_subject = 'Activate your account.'
message = render_to_string('core/acc_active_email.html', {
'user': user,'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': account_activation_token.make_token(user),
})
to_email = form.cleaned_data.get('email')
email = EmailMessage(
mail_subject, message, to=[to_email]
)
email.send()
return HttpResponse('Please confirm your email address to complete the registration')
forms.py
class Meta(UserCreationForm.Meta):
model = User
fields = ['username', 'email', 'password1', 'password2']
@transaction.atomic
def save(self):
user = super().save(commit=False)
user.is_student = True
user.save()
return user
答案1
得分: 1
问题在于你的表单中的 save
方法不接受 任何 参数,而你正在向它传递 commit=False
;你应该将参数添加到 save
方法中:
def save(self, <strong>commit=True, *args, **kwargs</strong>):
user = super().save(commit=False, *args, **kwargs)
user.is_student = True
user.save()
return user
通常情况下,当你重写内置方法时,应该保持方法签名与原始声明一致。
英文:
The problem is that your save
method in the form is not accepting any argument, and you are passing commit=False
to it; You should add the argument to the save
method:
<pre><code>def save(self, <strong>commit=True, *args, **kwargs</strong>):
user = super().save(commit=False, *args, **kwargs)
user.is_student = True
user.save()
return user</code></pre>
Generally, when you override a built-in method, you should keep the method signature as it was originally declared.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论