我想在django中创建一个自定义用户模型,但它给了我很多错误

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

我正在开发一个Django项目,所以我有调度应用程序,应用程序上的真正功能,我创建了一个“帐户”应用程序来修改基本用户模型,所以我可以让用户使用他/她的电子邮件登录。如果你可以帮助我解决这个问题,我会非常感激,因为我有点卡住了。但是,现在我修改了models.py(在“accounts”应用程序中)并修改了主网站文件夹中的settings.py,我收到以下错误(最后一个包括已安装的应用程序设置错误,我不知道理解:

回溯(最近一次调用最后一次):文件“C:/PythonProjects/tutorTrip/manage.py”,第15行,在execute_from_command_line(sys.argv)文件“C:\ Users \ Alienware \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ django \ core \ management__init __。py“,第371行,在execute_from_command_line utility.execute()文件”C:\ Users \ Alienware \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ django \ core \ management__init __。py“,第365行,执行self.fetch_command(子命令).run_from_argv(self.argv)文件”C:\ Users \ Alienware \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site -packages \ django \ core \ management \ base.py“,第288行,在run_from_argv self.execute(* args,** cmd_options)文件”C:\ Users \ Alienware \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ django \ core \ management \ commands \ runserver.py“,第61行,执行super()。执行(* args,** options)文件”C:\ Users \ Alienware \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ django \ core \ management \ base.py“,第335行,执行outpu t = self.handle(* args,** options)文件“C:\ Users \ Alienware \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ django \ core \ management \ commands \ runserver.py” ,第70行,句柄,如果不是settings.DEBUG而不是settings.ALLOWED_HOSTS:文件“C:\ Users \ Alienware \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ django \ conf__init __。py”,line 56,在getattr self._setup(name)文件“C:\ Users \ Alienware \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ django \ conf__init __。py”,第43行,在_setup self._wrapped =设置(settings_module)文件“C:\ Users \ Alienware \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ django \ conf__init __。py”,第120行,在init中提升ImproperlyConfigured(“%s设置”必须是列表或元组。 “%setting”django.core.exceptions.ImproperlyConfigured:INSTALLED_APPS设置必须是列表或元组。

进程以退出代码1结束

关注我的代码:

(账户/ models.py):

from django.db import models
from django.contrib.auth.models import (
    AbstractBaseUser, BaseUserManager 
)
   class UserManager(BaseUserManager):
       def create_user(self, email, password=None, active=True, 
is_staff=False, is_admin=False):
        if not email:
            raise ValueError("User must have an email")
       if not password:
            raise ValueError("Users must have a password")

        user = self.model(
            email=self.normalize_email(email),
        )
        user.set_password(password)  # change user password
        user.staff = is_staff
        user.admin = is_admin
       # user.active = is_active
       user.save(using=self._db)
       return user

def create_staffuser(self, email, password):
    # """
    # Creates and saves a staff user with the given email and password.
    # """
    user = self.create_user(
        email,
        password=password,
    )
    user.staff = True
    user.save(using=self._db)
    return user

def create_superuser(self, email, password):
    # """
    # Creates and saves a superuser with the given email and password.
    # """
    user = self.create_user(
        email,
        password=password,
    )
    user.staff = True
    user.admin = True
    user.save(using=self._db)
    return user


# user class
class User(AbstractBaseUser):
    email = models.EmailField(
        max_length=255,
        unique=True,
        verbose_name='email address',
    )
    active = models.BooleanField(default=True)  # can login
    staff = models.BooleanField(default=False)
    admin = models.BooleanField(default=False)

    USERNAME_FIELD = 'email'
    objects = UserManager()

    REQUIRED_FIELDS = []

    def __str__(self):
        return self.email

def get_full_name(self):
    return self.email

def get_short_name(self):
    return self.email

def has_module_perms(self, app_label):
    # "Does the user have permissions to view the app `app_label`?"
    # Simplest possible answer: Yes, always
    return True

def has_perm(self, perm, obj=None):
    # "Does the user have a specific permission?"
    # Simplest possible answer: Yes, always
    return True

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

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

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


class Profile(models.Model):
#user = models.OneToOneField(User)
# extend extra user data

(网站/ settings.py)

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__)))




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

ALLOWED_HOSTS = []

AUTH_USER_MODEL = 'accounts.User'
# Application definition

INSTALLED_APPS = {
    'accounts.apps.AccountsConfig',
    'scheduling.apps.SchedulingConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
}

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 = 'tutorTrip.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 = 'tutorTrip.wsgi.application'


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

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',
},
]

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


STATIC_URL = '/static/'

如果您需要任何额外的代码,请告诉我。 (也许不考虑注意力不集中,因为我在这里复制到代码框时有些麻烦,其中一些可能显示错误。)

python django python-3.x settings web-development-server
1个回答
2
投票

来自Django DOC - INSTALLED_APPS

指定在此Django安装中启用的所有应用程序的字符串列表。哪个说

但是,你被定义为INSTALLED_APPS作为设置对象({})。 {}语法用于表示集合对象

所以将INSTALLED_APPS改为如下

INSTALLED_APPS = [
    ..... # your apps
      ]

In [1]: a = {1,3}

In [2]: b= [1,3]

In [3]: c = (1,3)

In [4]: type(a)
Out[4]: set

In [5]: type(b)
Out[5]: list

In [6]: type(c)
Out[6]: tuple
© www.soinside.com 2019 - 2024. All rights reserved.