在这里使用的是Anaconda,直接使用Anaconda命令进行安装;
(E:\Anaconda3) C:\Users\yuan>conda install django
// 安装指定版本 django
(E:\Anaconda3) C:\Users\yuan>conda install django=1.9
// pip 安装指定版本 django
labelnet:~ yuan$ pip3 install django==1.9
当然你也可以看 官方文档
安装完成可以查看django版本 :
(E:\Anaconda3) C:\Users\yuan>python -m django --version
1.10.3
(1)使用下面命令创建Django工程
(E:\Anaconda3) D:\test>django-admin startproject mysite
(2)查看 mysite工程目录结构
(E:\Anaconda3) D:\test>tree /f
Folder PATH listing for volume data-one
Volume serial number is 000000AC 263B:D486
D:.
└─mysite #项目名称,算是个容器
│ manage.py #django的项目管理工具,通过命令行进行操作相关内容
│
└─mysite #与项目同名的总配置文件夹
settings.py #项目配置:app配置,数据配置等
urls.py #总的urls配置,可以配置app中的urls
wsgi.py # ?
__init__.py #一个空文件,告诉python是一个python包
(3)运行
使用下面命令运行: 默认端口为 8000
(E:\Anaconda3) D:\test\mysite>python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
November 24, 2016 - 11:10:07
Django version 1.10.3, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
当然,也可以指定端口,比如:
(E:\Anaconda3) D:\test\mysite>python manage.py runserver 8080
Performing system checks...
System check identified no issues (0 silenced).
You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
November 24, 2016 - 11:19:07
Django version 1.10.3, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8080/
Quit the server with CTRL-BREAK.
or 指定端口和IP
python manage.py runserver 0.0.0.0:8000
(4)效果
访问运行:http://127.0.0.1:8000/
(1)使用下面命令创建App
创建名为 testapp 的 app
(E:\Anaconda3) D:\test\mysite>python manage.py startapp testapp
(2)查看目录结构
(E:\Anaconda3) D:\test\mysite>tree /f
Folder PATH listing for volume data-one
Volume serial number is 0000006E 263B:D486
D:.
│ db.sqlite3 #创建app后,默认使用的是sqlite数据库,会生成该文件
│ manage.py
│
├─mysite
│ │ settings.py
│ │ urls.py
│ │ wsgi.py
│ │ __init__.py
│ │
│ └─__pycache__ #执行python脚本后,生成的可执行文件存储位置
│ settings.cpython-35.pyc
│ urls.cpython-35.pyc
│ wsgi.cpython-35.pyc
│ __init__.cpython-35.pyc
│
└─testapp #app名称
│ admin.py #管理员相关 : ?
│ apps.py #app相关: ?
│ models.py #模型,可定义数据库表,通过配置后,执行命令,生成数据库表; M :模型
│ tests.py #单元测试相关
│ views.py #主要以数据库操作相关 ;C : 控制器
│ __init__.py
│
└─migrations #models.py 中的表发生改变,生成数据库的时候,这里会有记录
__init__.py
(3)关于 pycache目录
当第一次运行 python
脚本时,解释器会将 *.py
脚本进行编译并保存到 __pycache__
目录 下次执行脚本时,若解释器发现你的 *.py
脚本没有变更,便会跳过编译一步,直接运行保存在 __pycache__
目录下的 *.pyc
文件
关闭 pycache
单次关闭 : 运行脚本时添加 -B 参数即可
永久关闭 : 设置环境变量 PYTHONDONTWRITEBYTECODE=1 即可
使用PyCharm进行开发,下载安装专业版,不要使用社区版(django各种不支持,可以写python);
专业版激活码 (来源 INTERNET):
43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZWVOYW1lIjoiIiwiYXNzaWduZWVFbWFpbCI6IiIsImxpY2Vuc2VSZXN0cmljdGlvbiI6IkZvciBlZHVjYXRpb25hbCB1c2Ugb25seSIsImNoZWNrQ29uY3VycmVudFVzZSI6ZmFsc2UsInByb2R1Y3RzIjpbeyJjb2RlIjoiSUkiLCJwYWlkVXBUbyI6IjIwMTctMDItMjUifSx7ImNvZGUiOiJBQyIsInBhaWRVcFRvIjoiMjAxNy0wMi0yNSJ9LHsiY29kZSI6IkRQTiIsInB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HzoQiuw4IqgGc45ohFH0UUyjYcuFxxDSU9lMCv8qdHKm+wnPRb0l9l5vXsCBDuhAGYD6ss+Ga+aDY6f/qXZuUCEUOH3QUNbbCUlviSz6+GiRnt1kA9N2Qachl+2yBfaqUqr8h7Z2gsx5LcIf5kYNsqJ0GavXTVyWh7PYiKX4bs354ZQLUwwa/cG++2+wNWP+HtBhVxMRNTdVhSm38AknZlD+PTAsWGu9GyLmhti2EnVwGybSD2Dxmhxk3IPCkhKAK+pl0eWYGZWG3tJ9mZ7SowcXLWDFAk0lRJnKGFMTggrWjV8GYpw5bq23VmIqqDLgkNzuoog==
File -> Open :项目文件夹 mysite , 非 app文件夹;
Tips
当然也可以使用PyCharm专业版 File -> New Project 进行创建项目