网站搭建-django-学习成绩管理-11-项目迁移

系统:Windows 7
语言版本:Anaconda3-4.3.0.1-Windows-x86_64
编辑器:pycharm-community-2016.3.2
Django:2.1.4
Python:3.6.0

  • 本系列介绍如何搭建一个网站,后端使用django框架
  • 今天开始介绍一个单独的项目app
  • 主要功能包括:学习成绩查询,数据统计分析
  • 涉及前端模块:Datatables、ECharts、JQuery

Part 1:目标

  • 因为近来重新换了一个电脑,将原来的代码直接复制到新电脑中是无法运行的。尝试重新创建数据库,进行migrate,各种报错。一怒之下,重新创建一次这个项目,进行手动迁移

Part 2:步骤

  1. 创建虚拟环境,安装Django,确保版本是和之前的时一致的
  2. 新建项目:django-admin startproject 项目名称,这里项目名称为A2,和之前的保持一致,所以对应命令为django-admin startproject A02
  3. 手动新建数据库:XUEXIHUOBAN
  4. 修改settings文件,配置数据库等,具体信息如下一节
  5. 数据库生成:makemigrationsmigrate,此时会生成自带工作表
  6. 新建python包,apps
  7. 复制之前项目的mediastatictemplate文件夹
  8. 新建app,python manage.py startapp school_grades
  9. pycharm中移动新建的app(school_grades)到apps文件夹下
    10.复制上一个项目该app下的py文件到新建的app下,注意只是复制py文件,另外两个文件夹不要复制
    11.数据库更新:makemigrationsmigrate,生成对应工作表
  10. 运行项目:python manage.py runserver

文件结构
网站搭建-django-学习成绩管理-11-项目迁移_第1张图片

py文件清单
网站搭建-django-学习成绩管理-11-项目迁移_第2张图片

运行结果
网站搭建-django-学习成绩管理-11-项目迁移_第3张图片

Part 3:A02-settings.py

"""
Django settings for A02 project.

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

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

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

import os
import sys

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))
sys.path.insert(0, os.path.join(BASE_DIR, 'extra_apps'))


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '省略'

# 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',
    'school_grades',
]

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


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

DATABASES = {
     
    'default': {
     
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'XUEXIHUOBAN',
        'USER': '账户',
        'PASSWORD': '密码',
        'HOST': '127.0.0.1'
    }
}

# Password validation
# https://docs.djangoproject.com/en/2.1/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/2.1/topics/i18n/

LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = False


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

STATIC_URL = '/static/'

STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)

MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace('\\', '/')     # 设置静态文件路径为主目录下的media文件夹
MEDIA_URL = '/media/'

Part 4:数据库迁移小技巧

  1. 当多次migrate,发现实际表和models中不一致,无法找到原因
  2. 提供一个比较暴力的解法
    • 先删除Mysql数据库中的该表,若有数据需要保留,请提前保存
    • models中对应该表的代码注释
    • 执行以下命令:
      • makemigrations
      • migrate --fake
    • 取消models中的对应表的注释
    • 执行以下命令
      • makemigrations
      • migrate

以上为本次的学习内容,下回见

长按图片识别二维码,关注本公众号
Python 优雅 帅气
12x0.8.jpg

你可能感兴趣的:(Python-django)