我正在尝试开发一个应用程序,其中有三种帐户类型学生登录,导师登录,机构登录我开发了三个名为的应用程序学生,导师和学院,但当我在学生应用程序中注册用户,然后通过该用户名我也可以登录到导师应用程序。那么如何管理所有类型的账户..
forms.py文件
class UserRegisterForm(forms.ModelForm):
#Student_Name= forms.CharField(max_length=300)
email = forms.EmailField(label="Email Address")
email2 = forms.EmailField(label="Confirm Email")
password = forms.CharField(widget=forms.PasswordInput)
password2 = forms.CharField(widget=forms.PasswordInput, label="Confirm Password")
class Meta:
model = User
fields = ['username','password','password2','email','email2']
def clean_password2(self):
print(self.cleaned_data)
password = self.cleaned_data.get('password')
password2 = self.cleaned_data.get('password2')
print(password, password2)
if password != password2:
raise forms.ValidationError("Passwords don't match")
else:
return password
def clean_email2(self):
print(self.cleaned_data)
email = self.cleaned_data.get('email')
email2 = self.cleaned_data.get('email2')
print(email, email2)
if email != email2:
raise forms.ValidationError("Email addresses don't match")
email_qs = User.objects.filter(email=email)
if email_qs.exists():
raise forms.ValidationError("This email has already been registered")
else:
return email
class UserLoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)
def clean(self, *args, **kwargs):
username = self.cleaned_data.get("username")
password = self.cleaned_data.get("password")
#user_qs = User.objects.filter(username=username)
#if user_qs.count() == 1:
# user = user_qs.first()
if username and password:
user = authenticate(username=username, password=password)
if not user:
raise forms.ValidationError("This user does not exist")
if not user.check_password(password):
raise forms.ValidationError("Incorrect password")
if not user.is_active:
raise forms.ValidationError("This user is no longer active")
return super(UserLoginForm, self).clean(*args, **kwargs)
views.py
def register_view(request):
print(request.user.is_authenticated())
form = UserRegisterForm(request.POST or None)
if form.is_valid():
user = form.save(commit=False)
password = form.cleaned_data.get('password')
user.set_password(password)
user.save()
new_user = authenticate(username=user.username, password=password)
login(request, new_user)
return render(request, 'app/register_sucessfull.html',{})
context = {
"form": form,
}
return render(request, "app/register.html", context)
def login_view(request):
print(request.user.is_authenticated())
w="Welcome"
title = "Login"
form = UserLoginForm(request.POST or None)
if form.is_valid():
username = form.cleaned_data.get("username")
password = form.cleaned_data.get("password")
user = authenticate(username=username, password=password)
login(request, user)
return render(request,'app/index1.html',{'username':username,'w':w})
#return redirect('student_view')
return render(request, "app/login.html", {"form":form, "title":title})
所有剩余应用中的相同代码
答案 0 :(得分:2)
我假设您希望通过角色区分用户。但你做的不是正确的方法。 Django有权限和组来解决这个问题。主动使用Django权限以显示或隐藏页面上的某些元素。行动也一样。相信我,您不希望为系统中的每个角色创建自定义身份验证位置。
创建Students
,Tutor
,Institute
django群组,然后根据权限更改您登录时显示的内容。这是唯一解决问题的Django方式
答案 1 :(得分:1)
您应该将Django auth Group用于三个不同的组,即学生,教师和学院,并为不同的登录分配不同的权限。您可以创建一个名为Permissions的新模型,并为所有gropus添加不同的permisions,请点击此处:
from django.contrib.auth.models import User, Group, Permission
from django.contrib.contenttypes.models import ContentType
content_type = ContentType.objects.get(app_label='yourapp', model='Your_model')
permission = Permission.objects.create(codename='can_login',
name='Can login as tutor ',
content_type=content_type)
user = User.objects.get(username='some_name')
group = Group.objects.get(name='tutor')
group.permissions.add(permission)
user.groups.add(group)
进一步阅读,Read from here.