我在Django应用程序中有多个用户模型,一个用于OLAROOM
,另一个用于REALTOR
。正如你所看到的,一切都很成功,但当我尝试验证他们并根据他们的角色重定向他们时,它不起作用。验证工作正常,但不是将Olaroom
用户重定向到房间页面,它将我重定向到Realtor_Dashboard
,并且用户角色不是房地产经纪人。使用下面的表单,根据保存函数中定义的内容创建用户。但是,当我尝试验证他们并根据他们的角色将用户重定向到页面时,它不起作用,我使用相同的方法,类似于以下内容:user.Role.REALTOR
和user.Role.OLAROOM
。请问我如何解决这个问题?
def login(request):
if request.method == 'POST':
form = AuthenticationForm(data=request.POST)
if form.is_valid():
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
if user.Role.REALTOR:
auth_login(request, user)
return redirect('Realtor_Dashboard')
elif user.Role.OLAROOM:
auth_login(request, user)
return redirect('Rooms')
else:
messages.error(request, 'Invalid Creadentials')
else:
form = AuthenticationForm()
return render(request, 'Login/login.html', {'form':form})
字符串
用户型号
class User(AbstractUser):
class Role(models.TextChoices):
ADMIN = "ADMIN", 'Admin'
REALTOR = "REALTOR", 'Realtor'
OLAROOM = "OLAROOM", 'Olaroom'
base_role = Role.ADMIN
role = models.CharField(max_length=225, choices=Role.choices)
username = models.CharField(max_length=15, unique=True)
first_name = models.CharField(max_length=225)
last_name = models.CharField(max_length=225)
email = models.EmailField(unique=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
def save(self, *args, **kwargs):
if not self.pk:
self.role = self.base_role
return super().save(*args, **kwargs)
def __str__(self):
return str(self.username)
class OlaroomsManger(BaseUserManager):
def get_queryset(self, *args, **kwargs):
results = super().get_queryset(*args, **kwargs)
return results.filter(role=User.Role.OLAROOM)
class Olaroom(User):
base_role = User.Role.OLAROOM
company_name = models.CharField(max_length=225)
address = models.CharField(max_length=225)
phone_number = models.CharField(max_length=15)
country = CountryField()
olaroom = OlaroomsManger()
def welcome(self):
return "Only for Olaroom"
class RealtorManger(BaseUserManager):
def get_queryset(self, *args, **kwargs):
results = super().get_queryset(*args, **kwargs)
return results.filter(role=User.Role.REALTOR)
class Realtor(User):
base_role = User.Role.REALTOR
address = models.CharField(max_length=225)
phone_number = models.CharField(max_length=15)
country = CountryField()
state = models.CharField(max_length=225)
realtor = RealtorManger()
def welcome(self):
return "Only for Realtor"
型
表单:
class OlaroomSignUpForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = Olaroom
fields = ['username', 'email', 'company_name', 'address', 'phone_number', 'country']
def save(self, commit=True):
user = super().save(commit=False)
user.Role.OLAROOM
if commit:
user.save()
return user
class RealtorSignUpForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = Realtor
fields = ['username', 'email', 'first_name', 'last_name', 'address', 'phone_number', 'country', 'state']
def save(self, commit=True):
user = super().save(commit=False)
user.Role.REALTOR
if commit:
user.save()
return user
型
1条答案
按热度按时间cgvd09ve1#
我面临的问题与我如何检查用户的角色并在身份验证后尝试重定向它们有关。在我的代码中,我检查
user.Role.REALTOR
和user.Role.OLAROOM
,但这不是基于我的模型检查用户角色的正确方法。相反,我应该检查用户示例的role属性,以确定它们的角色并相应地重定向。
字符串
在这段更新的代码中,我没有使用
user.Role.REALTOR
或user.Role.OLAROOM
,而是使用user.role == User.Role.REALTOR
和user.role == User.Role.OLAROOM
,这意味着:如果用户的角色是REALTOR
,它使用auth_login登录用户并重定向到Realtor_Dashboard
页面。如果用户角色是OLAROOM
,它登录用户并重定向到Rooms
页面。