Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python /accounts/register/uuu init_uuuu()处的TypeError获取了意外的关键字参数';实例';_Python_Django_Python 3.x - Fatal编程技术网

Python /accounts/register/uuu init_uuuu()处的TypeError获取了意外的关键字参数';实例';

Python /accounts/register/uuu init_uuuu()处的TypeError获取了意外的关键字参数';实例';,python,django,python-3.x,Python,Django,Python 3.x,在Django 2.0中尝试使用自定义用户模型注册用户时,出现以下错误 Environment: Request Method: GET Request URL: http://127.0.0.1:8000/accounts/register/ Django Version: 2.0.5 Python Version: 3.6.1 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'djang

在Django 2.0中尝试使用自定义用户模型注册用户时,出现以下错误

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/accounts/register/

Django Version: 2.0.5
Python Version: 3.6.1
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'crispy_forms',
 'homepage',
 'account']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "C:\Users\mittr\Desktop\MLASSI~1\mom\lib\site-packages\django\core\handlers\exception.py" in inner
  35.             response = get_response(request)

File "C:\Users\mittr\Desktop\MLASSI~1\mom\lib\site-packages\django\core\handlers\base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\mittr\Desktop\MLASSI~1\mom\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\mittr\Desktop\MLASSI~1\mom\lib\site-packages\django\views\generic\base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)

File "C:\Users\mittr\Desktop\ML Assignments\mom\src\account\views.py" in dispatch
  39.             return super(RegisterView, self).dispatch(*args, **kwargs)

File "C:\Users\mittr\Desktop\MLASSI~1\mom\lib\site-packages\django\views\generic\base.py" in dispatch
  89.         return handler(request, *args, **kwargs)

File "C:\Users\mittr\Desktop\MLASSI~1\mom\lib\site-packages\django\views\generic\edit.py" in get
  168.         return super().get(request, *args, **kwargs)

File "C:\Users\mittr\Desktop\MLASSI~1\mom\lib\site-packages\django\views\generic\edit.py" in get
  133.         return self.render_to_response(self.get_context_data())

File "C:\Users\mittr\Desktop\MLASSI~1\mom\lib\site-packages\django\views\generic\edit.py" in get_context_data
  66.             kwargs['form'] = self.get_form()

File "C:\Users\mittr\Desktop\MLASSI~1\mom\lib\site-packages\django\views\generic\edit.py" in get_form
  33.         return form_class(**self.get_form_kwargs())

Exception Type: TypeError at /accounts/register/
Exception Value: __init__() got an unexpected keyword argument 'instance'
url.py views.py Forms.py 其他资料 我转到
C:\Users\mittr\Desktop\ML Assignments\mom\Lib\site packages\django\views\generic\edit.py
,我看到第33行有以下内容:

`return form_class(**self.get_form_kwargs())`
当我将此更改为
返回表单_class()
时,注册页面成功显示。然而,我相信这不是做生意的最佳方式


此外,forms.py文件中的save()方法从未执行过,因此用户从未保存在自定义用户模型中。您的视图设置为使用UserRegistrationForm,但您的表单为UserRegistrationPerform

据推测,UserRegisterPerform是一个单独的表单,它不是modelform,也没有自定义的保存方法。您应该更改视图以指向正确的窗体

class RegisterView(CreateView):
    form_class = UserRegistrationForm
    template_name = 'registration/register.html'
    success_url = '/'

    def dispatch(self, *args, **kwargs):
        if self.request.user.is_authenticated:
            return redirect('/login')
        else:
            return super(RegisterView, self).dispatch(*args, **kwargs)
class UserRegisterForm(forms.ModelForm):

    password1 = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': 'Email'}))
    password2 = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': 'Email'}))

    class Meta:
        model = User
        fields = ['email', 'full_name']

    def clean_password2(self):
        password1 = self.cleaned_data.get('password1')
        password2 = self.cleaned_data.get('password2')

        if password1 and password2 and password1 != password2:
            raise forms.ValidationError("Passwords do not match.")
        else:
            return password2

    def clean_email(self):
        email = self.cleaned_data.get('email')
        qs = User.objects.get(email__iexact=email)
        if qs.exists():
            raise forms.ValidationError("A user with this email already exists.")
        else:
            return email

    def save(self, commit=True):
        user = super(UserRegisterForm, self).save(commit=False)
        user.set_password(self.cleaned_data['password1'])
        user.is_active = False

        if commit:
            user.save()
        return user
`return form_class(**self.get_form_kwargs())`