authenticate()函数仅用于验证超级用户
我曾经尝试过使用简单搜索,但是那样就不会使用login()函数了,也不会检查用户是否处于活动状态
'''
def Login_View(request):
if request.method == "POST":
username = request.POST['username']
paswrd = request.POST['pwd']
user = authenticate(username=username, password=paswrd)
if user is not None:
if user.is_active:
login(request, user)
name = User.objects.get(username=request.user)
request.session['username'] = username
return redirect('/dashboard/',{'name':name.username})
# return render(request,'dashboard_app/index.html',{'name':name.username})
else:
return render(request, 'login_app/index.html', {'error_message': 'Your account has been disabled'})
else:
return render(request, 'login_app/index.html', {'error_message': 'Invalid login'})
return render(request, 'login_app/index.html')
'''
我想登录数据库中的所有用户,无论是简单用户还是超级用户
答案 0 :(得分:0)
丹尼尔·罗斯曼,这是代码 表格 '''
class UserRegisterForm(UserCreationForm):
password1=forms.CharField(widget=forms.PasswordInput(attrs={'class':'form-control','placeholder':'Enter Password'}),label='Password')
password2=forms.CharField(widget=forms.PasswordInput(attrs={'class':'form-control','placeholder':'Confirm Password'}),label='Confirm Password')
class Meta:
model= User
fields= ('username','first_name','last_name','email', 'password1', 'password2',)
widgets={
'first_name': forms.TextInput(attrs={'class': 'form-control','placeholder':'First Name'}),
'last_name': forms.TextInput(attrs={'class': 'form-control','placeholder':'Last Name'}),
'username': forms.TextInput(attrs={'class': 'form-control','placeholder':'Username'}),
'email': forms.TextInput(attrs={'class': 'form-control','placeholder':'Email'}),
}
class Profile(forms.ModelForm):
class Meta:
model= Teacher
exclude=['t_teacher']
labels= {'t_gender':'Gender','t_departmant':'Department'}
widgets={
't_gender': forms.Select(choices="gender_list",attrs={'class': 'form-control'}),
't_departmant': forms.Select(choices="department_list",attrs={'class': 'form-control'}),
}
''' view.py '''
def Reg_View(request):
registered = False
if request.method == 'POST':
user_form = UserRegisterForm(data=request.POST)
profile_form=Profile(request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.refresh_from_db()
user.set_password(user.password)
user.first_name=user_form.cleaned_data.get('first_name')
user.last_name=user_form.cleaned_data.get('last_name')
user.email=user_form.cleaned_data.get('email')
user.save()
user.refresh_from_db()
name=User.objects.get(username=user.username)
p_save=profile_form.save()
p_save.refresh_from_db()
p_save.t_gender = profile_form.cleaned_data.get('t_gender')
p_save.t_departmant=profile_form.cleaned_data.get('t_departmant')
p_save.t_teacher=name
p_save.save()
registered = True
return redirect('/')
else:
print(user_form.errors, profile_form.errors)
else:
user_form = UserRegisterForm()
profile_form=Profile()
return render(request,'Login_app/regform.html',{'user_form':user_form, 'profile_form':profile_form ,'registered':registered })
'''
答案 1 :(得分:0)
该代码几乎是不必要的。特别是,这行没有意义:
user.set_password(user.password)
这将使用已经散列的密码,然后再次对其进行哈希处理。因此,现在它将永远无法通过身份验证。
您应该删除大部分代码。表格已经为您完成了。您应该拥有:
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
profile = profile_form.save(commit=False)
profile.t_teacher = user
profile.save()
return redirect('/')