Python Django不在数据库中存储密码
当我从视图中打印密码时,它会在命令提示符中显示non 我只在forms.py中使用password1 我把pass2设为无 Models.py:-Python Django不在数据库中存储密码,python,django,forms,django-models,view,Python,Django,Forms,Django Models,View,当我从视图中打印密码时,它会在命令提示符中显示non 我只在forms.py中使用password1 我把pass2设为无 Models.py:- class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) bio = models.TextField(max_length=500, blank=True)
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField(max_length=500, blank=True)
location = models.CharField(max_length=30, blank=True)
birth_date = models.DateField(null=True, blank=True)
gender = models.CharField(max_length=10,blank=True)
def __str__(self):
return self.fullname
class UserForm(forms.ModelForm):
username = forms.CharField(widget=forms.TextInput(attrs={'class':'validate','placeholder': 'Enter Username'}))
password1 = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder':'Enter Password'}))
email=forms.EmailField(widget=forms.TextInput(attrs={'placeholder':'Enter Email'}))
password2=None
class Meta:
model=User
fields=['username','password1','email']
class ProfileForm(forms.ModelForm):
fullname = forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Enter fullname'}))
class Meta:
model=Profile
fields=['fullname']
def register(request):
if request.method =='POST':
form = UserForm(request.POST)
profile_form = ProfileForm(request.POST)
if form.is_valid() and profile_form.is_valid():
user=form.save()
profile=profile_form.save(commit=False)
profile.user=user
profile.save()
username= form.cleaned_data.get('username')
password= form.cleaned_data.get('password')
print(username)
print(password)
messages.success(request,f'account created for {{ username }}')
return redirect('home')
else:
form = UserForm()
profile_form = ProfileForm()
context={'form':form , 'profile_form':profile_form}
return render(request, 'users/register.html',context)
Forms.py:-
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField(max_length=500, blank=True)
location = models.CharField(max_length=30, blank=True)
birth_date = models.DateField(null=True, blank=True)
gender = models.CharField(max_length=10,blank=True)
def __str__(self):
return self.fullname
class UserForm(forms.ModelForm):
username = forms.CharField(widget=forms.TextInput(attrs={'class':'validate','placeholder': 'Enter Username'}))
password1 = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder':'Enter Password'}))
email=forms.EmailField(widget=forms.TextInput(attrs={'placeholder':'Enter Email'}))
password2=None
class Meta:
model=User
fields=['username','password1','email']
class ProfileForm(forms.ModelForm):
fullname = forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Enter fullname'}))
class Meta:
model=Profile
fields=['fullname']
def register(request):
if request.method =='POST':
form = UserForm(request.POST)
profile_form = ProfileForm(request.POST)
if form.is_valid() and profile_form.is_valid():
user=form.save()
profile=profile_form.save(commit=False)
profile.user=user
profile.save()
username= form.cleaned_data.get('username')
password= form.cleaned_data.get('password')
print(username)
print(password)
messages.success(request,f'account created for {{ username }}')
return redirect('home')
else:
form = UserForm()
profile_form = ProfileForm()
context={'form':form , 'profile_form':profile_form}
return render(request, 'users/register.html',context)
views.py:-
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField(max_length=500, blank=True)
location = models.CharField(max_length=30, blank=True)
birth_date = models.DateField(null=True, blank=True)
gender = models.CharField(max_length=10,blank=True)
def __str__(self):
return self.fullname
class UserForm(forms.ModelForm):
username = forms.CharField(widget=forms.TextInput(attrs={'class':'validate','placeholder': 'Enter Username'}))
password1 = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder':'Enter Password'}))
email=forms.EmailField(widget=forms.TextInput(attrs={'placeholder':'Enter Email'}))
password2=None
class Meta:
model=User
fields=['username','password1','email']
class ProfileForm(forms.ModelForm):
fullname = forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Enter fullname'}))
class Meta:
model=Profile
fields=['fullname']
def register(request):
if request.method =='POST':
form = UserForm(request.POST)
profile_form = ProfileForm(request.POST)
if form.is_valid() and profile_form.is_valid():
user=form.save()
profile=profile_form.save(commit=False)
profile.user=user
profile.save()
username= form.cleaned_data.get('username')
password= form.cleaned_data.get('password')
print(username)
print(password)
messages.success(request,f'account created for {{ username }}')
return redirect('home')
else:
form = UserForm()
profile_form = ProfileForm()
context={'form':form , 'profile_form':profile_form}
return render(request, 'users/register.html',context)
**当我在viws中打印我的密码时,它显示无。另外,我只使用密码1**您使用的是django附带的用户模块
from django.contrib.auth import authenticate, login, get_user_model
from django.contrib.auth.models import User
Django用于以加密格式将数据存储在db.sqlite文件中。解码时需要一个密钥才能访问,该密钥位于settings.py文件中。您可以使用该密钥解码所有数据,但不能使用密码
密码将在sha256算法中哈希16次迭代,因此您无法获得密码,这是默认情况下完成的
如果要重置密码,请使用put调用重置密码问题在于您的用户表单
。
它需要以下字段:['username'、'password1'、'email']
从用户
模型,就好像它们存在一样
用户
型号没有名为的字段“password1”
。它确实有一个“password”
字段
因此,如果您将UserForm
Meta
类中的字段更改为['username'、'password'、'email']
,并将UserForm
中的“password1”字段更改为
现在,当您保存有效表单时,它实际上会将一个字段(“password”
)传递给用户模型,它需要该字段来生成您的哈希密码。password=form.cleaned_data.get('password')
->password=form.cleaned_data.get('password1'))
您的字段名为password1
,您试图从不存在的password
字段中获取数据。@h4z3我修复了它,但它不在数据库(我使用mysql)和管理面板中存储数据。我相信,因为要让用户模型验证密码,password1和password2必须相等。@Aerials如果我不提供password2怎么办?@RamPatil更改表单中密码字段的名称,从password1
到仅password
如果它解决了您的问题,请将此标记为答案。您执行此操作时,u=User.objects.get(username\uu-exact=username)打印u.password(这将获取哈希用户密码)您无法查看密码,因为它是哈希谢谢您的评论。我为password1编写了密码。谢谢。我通过将password1更改为password来修复它。在django表单中,它显示password1作为普通密码,password2用于重新输入相同的密码。