我使用Django。
我使用AbstractBaseUser在模型中创建用户。我为模型创建表格。我的表单没有在我的数据库中保存密码
表格:
from django import forms
from django.contrib.auth import get_user_model
class Register(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
password2 = forms.CharField(label='Confirm password', widget=forms.PasswordInput)
class Meta:
User = get_user_model()
model = User
fields = ('username', 'number' , 'country' , 'city' , 'email')
def clean_username(self):
User = get_user_model()
username = self.cleaned_data.get('username')
qs = User.objects.filter(username=username)
if qs.exists():
raise forms.ValidationError("username is taken")
return username
def clean_password2(self):
# Check that the two password entries match
password1 = self.cleaned_data.get("password")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords don't match")
return password2
class UserAdminCreationForm(forms.ModelForm):
"""A form for creating new users. Includes all the required
fields, plus a repeated password."""
password = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
class Meta:
User = get_user_model()
model = User
fields = ('username', 'number' , 'country' , 'city' , 'email')
def clean_password2(self):
# Check that the two password entries match
password1 = self.cleaned_data.get("password")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords don't match")
return password2
def save(self, commit=True):
# Save the provided password in hashed format
user = super(UserAdminCreationForm , self).save(commit=False)
user.set_password(self.cleaned_data["password"])
if commit:
user.save()
return user
观看次数:
@csrf_exempt
def Home(request):
if request.method == 'POST':
form = Register(request.POST)
if form.is_valid():
form.save()
return HttpResponse("greate")
else:
form = Register()
return render_to_response('home.html' , {'form' : form})
此操作在我的数据库中保存了电子邮件,用户名,号码以及国家和城市,但没有保存密码
请帮助我在数据库中保存密码