1. 建立虚拟环境 1.1 安装虚拟环境软件
1.2 克隆一个环境为虚拟环境
1 2 virtualenv python2.7 cd python2.7
2. 安装Django 1 2 pip install django==1.11.3
3. 创建项目 1 2 django-admin startproject mytest cd mytest
4. 创建表结构 1 python manage.py migrate
5. 可以进行简单的测试 1 2 3 python manage.py runserver
6. 创建超级管理员用户 1 2 python manage.py createsuperuser
7. 登录admin的后台
8. 创建app应用 1 python manage.py startapp myapp
9. 将app配置为project-mytest的app 1 2 3 4 5 6 7 8 9 10 11 12 from __future__ import unicode_literalsfrom django.shortcuts import renderfrom django.http import HttpResponsedef index (request) : return HttpResponse("Hello, world! You're at my test app index!" )
1 2 3 4 5 6 7 8 from django.conf.urls import urlfrom . import viewsurlpatterns = [ url('' , views.index, name='index' ), ]
1 2 3 4 5 6 7 8 9 10 11 12 13 INSTALLED_APPS = [ 'django.contrib.admin' , 'django.contrib.auth' , 'django.contrib.contenttypes' , 'django.contrib.sessions' , 'django.contrib.messages' , 'django.contrib.staticfiles' , 'myapp.apps.MyappConfig' , ]
1 2 3 4 5 6 7 8 9 from django.conf.urls import url, includefrom django.contrib import adminurlpatterns = [ url(r'^admin/' , admin.site.urls), url(r'^myapp/' , include('myapp.urls' )), ]
10. 测试新增的myapp是否可用 1 2 3 python manage.py runserver
–填坑 – 当前的Django版本无法引入path,所以官网的教程需要针对2.0版本,本版本可以用url来替代path获取路径以及加载等1 2 3 4 from django.conf.urls import urlurlpatterns = [ url('' , views.index, name='index' ) ]