无法通过Django的管理页面登录

问题描述 投票:0回答:1

我已经创建了用于概要分析和身份验证的自定义用户模型,但是它似乎无法对密码进行身份验证。我尝试了无数次更改它,甚至在shell上确认了它,但在Django Admin上仍然没有通过。

这是我的模型。py:

from django.db import models
from django.contrib.auth.models import (
    BaseUserManager, AbstractBaseUser
)

# Create your models here.


class UserManager(BaseUserManager):
    def create_user(self, email, password=None, user_type=None, is_superuser=False, is_pqa=False, is_staff=False, is_admin=False, is_test_lead=False, is_test_manager=False, is_active=True):
        if not email:
            raise ValueError("User must have an email address!")
        if not password:
            raise ValueError("User must have a password!")
        user_obj = self.model(
            email=self.normalize_email(email)
        )
        user_obj.set_password(password)
        user_obj.user_type = user_type
        user_obj.ad = is_admin
        user_obj.superuser = is_superuser
        user_obj.tm = is_test_manager
        user_obj.pqa = is_pqa
        user_obj.tl = is_test_lead
        user_obj.active = is_active
        user_obj.save(using=self._db)
        return user_obj

    def create_pqa(self, email, password=None):
        user = self.create_user(
            email,
            password=password,
            is_pqa=True,
            is_staff=True,
            user_type=1
        )
        return user

    def create_staff(self, email, password=None):
        user = self.create_user(
            email,
            password=password,
            is_staff=True,
            user_type=2
        )
        return user

    def create_test_lead(self, email, password=None):
        user = self.create_user(
            email,
            password=password,
            is_test_lead=True,
            is_staff=True,
            user_type=3
        )
        return user

    def create_test_manager(self, email, password=None):
        user = self.create_user(
            email,
            password=password,
            is_test_manager=True,
            is_staff=True,
            user_type=4
        )
        return user

    def create_superuser(self, email, password=None):
        user = self.create_user(
            email,
            password=password,
            is_admin=True,
            is_superuser=True,
            is_staff=True,
            user_type=5
        )
        return user


class User(AbstractBaseUser):
    USER_TYPE_CHOICES = (
        (1, 'pqa'),
        (2, 'tester'),
        (3, 'test_lead'),
        (4, 'test_manager'),
        (5, 'admin'),
    )
    email = models.EmailField(max_length=255, unique=True)
    full_name = models.CharField(max_length=255, blank=True, null=True)
    active = models.BooleanField(default=True)
    birth_date = models.DateField(null=True, blank=True)
    hire_date = models.DateField(null=True, blank=True)
    user_type = models.PositiveSmallIntegerField(choices=USER_TYPE_CHOICES)
    ad = models.BooleanField(default=False)  # superuser/admin
    # non-superuser but with Team Manager Privilages
    tm = models.BooleanField(default=False)
    # non-superuser but with PQA Privilages
    pqa = models.BooleanField(default=False)
    ts = models.BooleanField(default=False)  # non-superuser
    # non-superuser but with TL Privilages
    tl = models.BooleanField(default=False)
    superuser = models.BooleanField(default=False)
    timestamp = models.DateTimeField(auto_now_add=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = UserManager()

    def __str__(self):
        return self.full_name

    def get_full_name(self):
        return self.full_name

    def get_short_name(self):
        return self.full_name

    @property
    def is_pqa(self):
        return self.pqa

    @property
    def is_staff(self):
        return self.ts

    @property
    def is_test_lead(self):
        return self.tl

    @property
    def is_test_manager(self):
        return self.tm

    @property
    def is_admin(self):
        return self.ad

    @property
    def is_active(self):
        return self.active

    @property
    def is_superuser(self):
        return self.superuser

这是我的settings.py:

"""
Django settings for ceres project.

Generated by 'django-admin startproject' using Django 3.0.3.

For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '@3xm&$=ky!sq2_i$-9cd%48ork1l$1y-+e__g_4d!+3*2ssli-'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'profiles.apps.ProfilesConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

AUTH_USER_MODEL = 'profiles.User'  # Custom model

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'ceres.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'ceres.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = '/static/'

我真的很感谢有人为我指出正确的方向,因为我很茫然。预先感谢!

python sql django authentication logout
1个回答
1
投票

通过创建新的超级用户

python manage.py createsuperuser

您将可以登录

© www.soinside.com 2019 - 2024. All rights reserved.