【发布时间】:2016-02-11 23:52:27
【问题描述】:
我创建了一个Custom User model in Django,您可以在下面看到我的models.py。
from django.core.exceptions import ObjectDoesNotExist
from django.conf import settings
from uuidfield import UUIDField
class User(AbstractUser, Mixin):
objects = HiddenUserManager()
USERNAME_FIELD = 'username'
# REQUIRED_FIELDS = ['email', 'password']
api_token = UUIDField(auto=True)
token_created_date = models.DateTimeField(auto_now_add=True)
class Meta:
unique_together = ('email', ) # must have unique email!
app_label = 'portal'
def api_token_reset(self):
self.api_token = UUIDField(auto=True)
我还更改了我的settings.py 文件并添加了这一行:
AUTH_USER_MODEL = 'portal.User'portal 是我的应用程序的名称。
我使用portal.user model 创建了两个superusers,如下图所示:
问题是当我在索引登录页面中输入我的username and password 时,它无法对我进行身份验证。创建模型后,我执行了所有migrations。为什么登录页面无法验证我的身份?
如果我注释掉我的customer user model 并删除这一行AUTH_USER_MODEL = 'portal.User',那么我的登录页面就可以对我进行身份验证。知道我哪里出错了吗?
我的登录视图如下所示:
from django.contrib.auth.views import login
from django.http import HttpResponseRedirect
def login(request, **kwargs):
return login(request, **kwargs)
有人可以帮我吗?
更新:混合类
class RequireStaffMixinView(RequireAuthMixinView):
# Override 1) ensure staff
# @method_decorator(staff_user_required)
def dispatch(self, *args, **kwargs):
if not self.request.user.is_staff:
raise PermissionDenied
return super(RequireAuthMixinView, self).dispatch(*args, **kwargs
【问题讨论】:
-
您能否发表您的观点,其中有处理身份验证的逻辑代码,例如
authenticate方法,好吗? -
user.is_authenticated正在工作,但我的用户应该是superuser才能访问下一页。查看我使用的mixin类,以确保用户始终使用superuser访问下一页或提出Permission Denied -
混合在 User 模型中的作用是什么?
-
确保用户模型为
superuser,否则提升PermissionDenied -
关于登录功能,我使用的是
from django.contrib.auth.views import login
标签: django authentication django-models django-templates django-views