我创建了Custom User model in Django
,并且您在下面看到我的models.py
。无法在Django中验证自定义用户模型
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'
门户网站是我的应用程序的名称。
我已经创建了两个superusers
使用portal.user model
,你可以下面的图片中看到:
问题是,当我进入我的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'正在工作,但我的用户应该是'超级用户'来访问下一页。查看我正在使用的'mixin'类,以确保用户始终是'超级用户'来访问下一页或者提高'权限被拒绝' – python
在用户模型中混用的是什么? – utkbansal