我創建了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