查看多个表单不保存(在表单上缺少1个位置参数)

时间:2016-07-25 22:36:12

标签: django django-forms django-templates django-registration

我已经为Django-Registration-Redux设置了一个覆盖,以保存下面的其他模型UserProfile:

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    tel_number = models.CharField(max_length=20, null = True)

使用以下格式进行设置:

class UserProfileRegistrationForm(RegistrationFormUniqueEmail):
    tel_number = forms.CharField()

并且regbackend.py:

from registration.backends.default.views import RegistrationView
from .forms import UserProfileRegistrationForm
from .models import UserProfile

class MyRegistrationView(RegistrationView):

    form_class = UserProfileRegistrationForm

    def register(self, request, form_class):
        new_user = super(MyRegistrationView, self).register(request, form_class)
        user_profile = UserProfile()
        user_profile.user = new_user
        user_profile.tel_number = form_class.cleaned_data['tel_number']
        user_profile.save()
        return user_profile

但是,即使视图正确加载并且我可以填写它,每次保存时都会出现以下错误:

register() missing 1 required positional argument: 'form_class'

我认为这是错误的传递?

1 个答案:

答案 0 :(得分:1)

我在这里快速检查了代码:

NaN

Django-Registration-Redux似乎在RegistrationView中实现,寄存器方法如下:

def register(self, form):
   #code

尝试删除请求,它应该有效,就像这样。

def register(self, form_class):
   new_user = super(MyRegistrationView, self).register(form_class)
   #code