Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.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 create_user()接受2到4个位置参数,但给出了6个_Python_Django - Fatal编程技术网

Python create_user()接受2到4个位置参数,但给出了6个

Python create_user()接受2到4个位置参数,但给出了6个,python,django,Python,Django,填写注册表并单击“提交”后,会弹出此错误 “create_user()接受2到4个位置参数,但给出了6个” 而且它也不在模板的表单中使用验证 不检查用户名或电子邮件是否已在数据库中,甚至不检查密码是否匹配 forms.py from django import forms from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.models import User from django.fo

填写注册表并单击“提交”后,会弹出此错误 “create_user()接受2到4个位置参数,但给出了6个” 而且它也不在模板的表单中使用验证

不检查用户名或电子邮件是否已在数据库中,甚至不检查密码是否匹配

forms.py

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.forms.widgets import PasswordInput, TextInput
from django.core.exceptions import ValidationError



class CustomUserCreationForm(forms.Form):
    username = forms.CharField(widget=TextInput(attrs={'class':'validate','id': 'icon_prefix', 'type': 'text'}), min_length=4, max_length=150)
    email = forms.EmailField(widget=TextInput(attrs={'class':'validate','id': 'icon_prefix', 'type': 'text'}))
    first_name = forms.CharField(widget=TextInput(attrs={'class':'validate','id': 'icon_prefix', 'type': 'text'}), min_length=2, max_length=150)
    last_name = forms.CharField(widget=TextInput(attrs={'class':'validate','id': 'icon_prefix', 'type': 'text'}), min_length=2, max_length=150)
    password1 = forms.CharField(widget=PasswordInput(attrs={'class':'validate','id': 'icon_prefix', 'type': 'password'}))
    password2 = forms.CharField(widget=PasswordInput(attrs={'class':'validate','id': 'icon_prefix', 'type': 'password'}))



def clean_username(self):
    username = self.cleaned_data['username'].lower()
    r = User.objects.filter(username=username)
    if r.count():
        raise  ValidationError("Username already exists")
    return username

def clean_first_name(self):
    first_name = self.cleaned_data['first_name']

    return first_name

def clean_last_name(self):
    last_name = self.cleaned_data['last_name']

    return last_name

def clean_email(self):
    email = self.cleaned_data['email'].lower()
    r = User.objects.filter(email=email)
    if r.count():
        raise  ValidationError("Email already exists")
    return email

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

    if password1 and password2 and password1 != password2:
        raise ValidationError("Password don't match")

    return password2

def save(self, commit=True):
    user = User.objects.create_user(
        self.cleaned_data['username'],
        self.cleaned_data['first_name'],
        self.cleaned_data['last_name'],
        self.cleaned_data['email'],
        self.cleaned_data['password1']
    )
    return user
只需要用户名作为位置参数。所有其他数据应作为关键字参数传递

user = User.objects.create_user(
    self.cleaned_data['username'],
    first_name=self.cleaned_data['first_name'],
    last_name=self.cleaned_data['last_name'],
    email=self.cleaned_data['email'],
    password=self.cleaned_data['password1']
)
你不应该使用