我正在尝试在Django中创建自定义用户模型,但这给了我很多错误

时间:2018-09-10 06:01:26

标签: python django python-3.x settings web-development-server

我正在开发Django项目,因此我具有调度应用程序,该应用程序上的实际功能,并且创建了一个“帐户”应用程序来修改基本用户模型,因此我可以使用户使用他/的身份登录她的电子邮件。如果您能帮助我解决这个问题,我将不胜感激,因为我有些执着。 但是,现在,我修改了models.py(在“帐户”应用中)并修改了主网站文件夹上的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(subcommand).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()。execute(* args,** options)文件“ C:\ Users \ Alienware \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ django \ core \ management \ base.py”,   执行中的第335行       输出= 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”,    getattr 中的第56行       self._setup(name)文件“ C:\ Users \ Alienware \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ django \ conf__init __。py”,   _setup中的第43行       self._wrapped =设置(设置模块)文件“ C:\ Users \ Alienware \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ django \ conf__init __。py”,   第120行,初始化       提高ImproperlyConfigured(“%s设置必须是列表或元组。”%设置)django.core.exceptions.ImproperlyConfigured:   INSTALLED_APPS设置必须是列表或元组。

     

以退出代码1完成的过程

遵循我的代码:

(accounts / 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

(website / 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/'

如果您需要任何其他代码,请告诉我。 (也许不要考虑这种束缚,因为我在复制到此处的代码箱时遇到了一些麻烦,其中一些可能显示错误。)

1 个答案:

答案 0 :(得分:0)

解决方案是您收到的ErrorMessage,内容为

ImproperlyConfigured("The %s setting must be a list or a tuple. " % setting)
django.core.exceptions.ImproperlyConfigured: The INSTALLED_APPS setting must be a list or a tuple.

通过将{}更改为[],将已安装的应用程序数据类型更改为列表。