第三百七十三节,Django+Xadmin打造上线标准的在线教育平台—创建用户app,在models.py文件生成3张表,用户表、验证码表、轮播图表

第三百七十三节,Django+Xadmin打造上线标准的在线教育平台—创建用户app,在models.py文件生成3张表,用户表、验证码表、轮播图表

 

创建Django项目

项目

 

settings.py配置

配置数据库

"""
Django settings for MxOnline project.

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

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

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/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/1.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '!#-519=(t8yl=of8^u$(zdcfcovctqlh0n2p#fky&9c3la+j1k'

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

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    '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 = 'MxOnline.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')]
        ,
        '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 = 'MxOnline.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
#
# DATABASES = {
#     'default': {
#         'ENGINE': 'django.db.backends.sqlite3',
#         'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
#     }
# }

#MySQL数据库
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',       # 配置数据库引擎名称
        'NAME': 'mxonline',                         # 数据库名称
        'USER': 'root',                             # 数据库用户名
        'PASSWORD': '279819',                       # 数据库密码
        'HOST': '127.0.0.1',                        # 数据库链接地址
        'PORT': '3306',                             # 数据库端口
    }
}



# Password validation
# https://docs.djangoproject.com/en/1.10/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/1.10/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/1.10/howto/static-files/

STATIC_URL = '/static/'

创建数据库

 

 

创建用户相关app,名称叫app_users

 

开始编写用户相关名称叫app_users的app数据库操作models.py文件

 

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from __future__ import unicode_literals
from django.db import models        # 导入models对象


class app_users(models.Model):       # 创建类必须继承models.Model,类名将是在数据库里的表名称
    password = models.CharField(max_length=128, verbose_name='密码', default='', blank=False)         # 密码字段,长度128,默认值为空字符,前端不允许用户输入空
    last_login = models.DateTimeField(verbose_name='登录日期', null=True)                           # 允许为空
    is_superuser = models.BooleanField(max_length=1, verbose_name='用户身份', name=False)
    username = models.CharField(max_length=150, verbose_name='用户名', null=False, blank=False)
    first_name = models.CharField(max_length=30, verbose_name='拓展1', null=False)
    last_name = models.CharField(max_length=30, verbose_name='拓展2', null=False)
    email = models.EmailField(max_length=254, verbose_name='邮箱', null=False, blank=False)
    is_staff = models.BooleanField(max_length=1, verbose_name='是否是员工', null=False)
    is_active = models.BooleanField(max_length=1, verbose_name='是否激活', null=False)
    date_joined = models.DateTimeField(verbose_name='注册日期', null=True)
    nick_name = models.CharField(max_length=50, verbose_name='昵称', default='')
    birday = models.DateField(verbose_name='生日', null=True)
    gender = models.CharField(max_length=3, verbose_name='性别', choices=(("male", ""), ("female", "")), default='male')
    address = models.CharField(max_length=100, verbose_name='地区', default='')
    mobile = models.CharField(max_length=11, verbose_name='手机', null=True, blank=True)
    image = models.ImageField(upload_to='image/%Y/%m', verbose_name='头像', default='image/default.png', max_length=100)

    class Meta:
        verbose_name = '用户信息表'
        verbose_name_plural = verbose_name

 

 

将名称叫app_users的app注册

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app_users',                        # 注册 APP
]

 

执行命令生成数据库表

 

我们继续完成名称叫app_users的app数据库操作models.py文件,我们在这个models.py文件操作3张表

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from __future__ import unicode_literals
from datetime import datetime

from django.db import models        # 导入models对象


class Users(models.Model):       # 创建类必须继承models.Model,类名将是在数据库里的表名称
    password = models.CharField(max_length=128, verbose_name='密码', default='', blank=False)         # 密码字段,长度128,默认值为空字符,前端不允许用户输入空
    last_login = models.DateTimeField(verbose_name='登录日期', null=True)                           # 允许为空
    is_superuser = models.BooleanField(max_length=1, verbose_name='用户身份', name=False)
    username = models.CharField(max_length=150, verbose_name='用户名', null=False, blank=False)
    first_name = models.CharField(max_length=30, verbose_name='拓展1', null=False)
    last_name = models.CharField(max_length=30, verbose_name='拓展2', null=False)
    email = models.EmailField(max_length=254, verbose_name='邮箱', null=False, blank=False)
    is_staff = models.BooleanField(max_length=1, verbose_name='是否是员工', null=False)
    is_active = models.BooleanField(max_length=1, verbose_name='是否激活', null=False)
    date_joined = models.DateTimeField(verbose_name='注册日期', null=True)
    nick_name = models.CharField(max_length=50, verbose_name='昵称', default='')
    birday = models.DateField(verbose_name='生日', null=True)
    gender = models.CharField(max_length=3, verbose_name='性别', choices=(("male", ""), ("female", "")), default='male')
    address = models.CharField(max_length=100, verbose_name='地区', default='')
    mobile = models.CharField(max_length=11, verbose_name='手机', null=True, blank=True)
    image = models.ImageField(upload_to='image/%Y/%m', verbose_name='头像', default='image/default.png', max_length=100)

    class Meta:
        verbose_name = '用户信息表'
        verbose_name_plural = verbose_name


class Email(models.Model):
    code = models.CharField(max_length=20, verbose_name='验证码')
    email = models.EmailField(max_length=50, verbose_name='邮箱')
    send_type = models.CharField(max_length=10, choices=(('register', '注册'), ('forget', '找回密码')), verbose_name='邮箱验证类型')
    send_time = models.DateTimeField(verbose_name='生成时间', default=datetime.now)

    class Meta:
        verbose_name = '用户邮箱验证码表'
        verbose_name_plural = verbose_name


class Banner(models.Model):
    title = models.CharField(max_length=100, verbose_name='轮播图标题')
    image = models.ImageField(upload_to='banner/%Y/%m', verbose_name='轮播图片', max_length=100)  # 图片路径banner/%Y/%m  /年/月
    url = models.URLField(max_length=200, verbose_name='轮播图访问地址')
    index = models.ImageField(default=100, verbose_name='轮播图顺序')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='轮播图添加时间')

    class Meta:
        verbose_name = '网站轮播图表'
        verbose_name_plural = verbose_name

 

posted @ 2017-09-07 22:36  林贵秀  阅读(1014)  评论(1编辑  收藏  举报