Django注册并确认电子邮件,错误:save()收到了意外的关键字参数’commit’。

huangapple go评论59阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年1月3日 13:45:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/59573729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定