《Django By Example》读书笔记 01
2016-04-21 15:28
585 查看
一、Django环境搭建
系统环境 windows 10 64位
安装python3.4
https://www.python.org/
安装Django
https://docs.djangoproject.com/
在cmd窗口输入
pip install Django==1.9.5
二、创建第一个工程
在工作目录下创建Django文件夹,cmd进入Django路径下,输入:
django-admin startproject mammothsite
创建mammothsite project
cd mammothsite
python manage.py migrate
然后会看到
运行服务:
python manage.py runserver
会看到以下提示
在浏览器输入http://127.0.0.1:8000/
可以看到:
下面进入mammothsite子文件夹看看
settings.py
这里的DEBUG 是调试模式的开关,当设置为True时,当project发生异常会显示详细的异常信息。但是在上线的时候应该将其关掉,防止配置信息或者敏感出错信息泄露。
ALLOWED_HOSTS是为了限定请求中的host值,以防止黑客构造包来发送请求,只有在列表中的host才能访问.强烈建议不要使用*通配符去配置,另外当DEBUG设置为False的时候必须配置这个配置.否则会抛出异常。
INSTALLED_APPS是一个一元数组,里面是应用中要加载的自带或者自己定制的app包路径列表。
默认的包括
django.contrib.admin: This is an administration site.
django.contrib.auth: This is an authentication framework.
django.contrib.contenttypes: This is a framework for content types.
django.contrib.sessions: This is a session framework.
django.contrib.messages: This is a messaging framework.
django.contrib.staticfiles: This is a framework for managing static files.
MIDDLEWARE_CLASSES:web应用中需要加载的一些中间件列表。是一个一元数组.里面是django自带的或者定制的中间件包路径,如下:
数据库的设置以后再详细说
其它设置项使用时再查阅相关资料
系统环境 windows 10 64位
安装python3.4
https://www.python.org/
安装Django
https://docs.djangoproject.com/
在cmd窗口输入
pip install Django==1.9.5
二、创建第一个工程
在工作目录下创建Django文件夹,cmd进入Django路径下,输入:
django-admin startproject mammothsite
创建mammothsite project
cd mammothsite
python manage.py migrate
然后会看到
Rendering model states... DONE Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying sessions.0001_initial... OK
运行服务:
python manage.py runserver
会看到以下提示
Performing system checks... System check identified no issues (0 silenced). November 5, 2015 - 19:10:54 Django version 1.8.6, using settings 'mysite.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
在浏览器输入http://127.0.0.1:8000/
可以看到:
下面进入mammothsite子文件夹看看
settings.py
""" Django settings for mammothsite project. Generated by 'django-admin startproject' using Django 1.9.5. For more information on this file, see https://docs.djangoproject.com/en/1.9/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/1.9/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.9/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '9*w=fvrr3b7ntp44$=g2cj2itpn*nc0@mqf)g4xqxnw)&qqqn!' # 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_CLASSES = [ 'django.middleware.security.SecurityMiddleware', '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 = 'mammothsite.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 = 'mammothsite.wsgi.application' # Database # https://docs.djangoproject.com/en/1.9/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } # Password validation # https://docs.djangoproject.com/en/1.9/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.9/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.9/howto/static-files/ STATIC_URL = '/static/'
这里的DEBUG 是调试模式的开关,当设置为True时,当project发生异常会显示详细的异常信息。但是在上线的时候应该将其关掉,防止配置信息或者敏感出错信息泄露。
ALLOWED_HOSTS是为了限定请求中的host值,以防止黑客构造包来发送请求,只有在列表中的host才能访问.强烈建议不要使用*通配符去配置,另外当DEBUG设置为False的时候必须配置这个配置.否则会抛出异常。
INSTALLED_APPS是一个一元数组,里面是应用中要加载的自带或者自己定制的app包路径列表。
默认的包括
django.contrib.admin: This is an administration site.
django.contrib.auth: This is an authentication framework.
django.contrib.contenttypes: This is a framework for content types.
django.contrib.sessions: This is a session framework.
django.contrib.messages: This is a messaging framework.
django.contrib.staticfiles: This is a framework for managing static files.
MIDDLEWARE_CLASSES:web应用中需要加载的一些中间件列表。是一个一元数组.里面是django自带的或者定制的中间件包路径,如下:
MIDDLEWARE_CLASSES = [ 'django.middleware.security.SecurityMiddleware', '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', ]
数据库的设置以后再详细说
其它设置项使用时再查阅相关资料
相关文章推荐
- 百度地图隐藏缩放控件比例尺Logo
- 百度地图隐藏缩放控件比例尺Logo
- go access database demo
- 用cargo插件部署war包,并支持debug
- Maven使用Cargo实现自动化部署
- 机器学习算法笔记2_1:生成学习算法(Generative Learning algorithms)
- GoSublime 快捷键
- GoSublime介绍安装配置
- go beego install
- 十条有用的 GO 技术
- GoLang 并发控制
- goLang 如何设置捕获错误
- GoLang package解释
- goLang 位左移
- gocode 安装
- go time package format 时间格式
- goLang 特殊符号优先级表
- go语言25个关键字总结
- 深入学习golang—channel
- Google Go 学习资源