软件测试之路测试Inbox

Django之旅:2

2018-01-05  本文已影响16人  五娃儿

包含内容

创建Django应用

实现具体功能的模块被称为应用,一个项目里可以有多个应用及application
操作步骤:
1、进入到mysite目录即manage.py文件所在路径
2、执行python manage.py startapp blog
3、通过ls 查看mysite目录下的所有内容

命令.png
目录结构.png

Ps: blog为项目mysite的一个应用,应用创建后,Django会自动增加一些文件

基础文件说明

2、 功能区别
manage.py在命令上是django-admin的升级版,里面既包含django-admin命令也有manage自己的独特命令


manage&django-admin命令对比.png

特殊说明

将setting.py文件列为特殊说明,是因为在整个django项目中该文件很重要

"""
#在setting顶部说明这个mysite项目的django配置文件
Django settings for mysite project.

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

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

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

import os #导入需要的包

BASE_DIR = os.path.dirname(os.path.dirname(__file__))#获取BASE_DIR路径


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '6lx63@#l07h$d^@pc=h-4due2-21$@m268+=5sxqfeft^god)h'

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

DEBUG = True #在开发过程中,值为True,会展示详细的错误,具体到行,部署到生产环境时,设置为False

TEMPLATE_DEBUG = True# 当其值为True时 会展示具体的模板错误,为False时则不展示决提的错误模块代码,不利于调试

ALLOWED_HOSTS = []# 默认为空,此Django站点可以提供服务的主机/域名的字符串列表 


# Application definition,应用注册列表,每新增加一个应用则需要在此注册

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'mysite.urls'

WSGI_APPLICATION = 'mysite.wsgi.application'


# Database,数据库配置,默认为sqlite3
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

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

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

STATIC_URL = '/static/'# 静态文件路径

知识点:

1、通过python manage.py startapp appname 或者django-admin startapp appname 创建应用
2、基础配置文件作用、应用文件功能
3、setting文件进行应用的注册、数据库配置、调试信息配置、模板、静态文件配置

学习来源:跟老齐学Python:Django
目录

上一篇 下一篇

猜你喜欢

热点阅读