环境:WINDOWS+配置好的python环境
由此Django的大坑开始了
Chapter One:配置开发环境
配置开发环境的必备过程:将django加入到环境变量
\Python35\Lib\site-packages\Django-1.11-py3.5.egg\django\bin
加入到环境变量PATH中
安装完在命令行运行一下
python
>>>importd django
>>>print(django.VERSION)
>>>(1, 11, 0, 'alpha', 0)
说明django安装成功
Chapter Two:新建项目
在目标命令创建一个项目(project)
在目标目录shift+右键→在此处运行命令行
django-admin startproject mysite
此处是一大坑,网上教程大多为
django-admin.py startproject mysite
然后会造成报错
Type 'django-admin.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runserver
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
Note that only Django core commands are listed as settings are not properly
configured (error: Requested setting INSTALLED_APPS, but settings are not
configured. You must either define the environment variable
DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).
应该是较新版的django的配置方法不同(?)..
___
Chapter Three:新建子应用(app)
--
同理新建一个app,用来处理对主页的访问
django-admin startapp index
于是路径结构变成了
![新建app:index之后的路径](http://upload-images.jianshu.io/upload_images/3996362-2ec3d49e0074882c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在settings.py中注册该app:
![添加app至settings.py](http://upload-images.jianshu.io/upload_images/3996362-ffb650ff5b7919e3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
___
Chapter Four:简易回应一个请求
--
构造一个函数型的response,返回对后台的请求
在./index/views.py中声明一个函数
from django.http import HttpResponse
def index(request):
return HttpResponse('Hello world')
还没完成,在./mysite/urls.py中创建指向刚才写好的返回函数的链接
可以看到urls.py中对如何写url的注释:
![如何写一个url到urls.py内部](http://upload-images.jianshu.io/upload_images/3996362-115323e60cb4f52b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
我们刚才构造index(request)的方法属于Function views,所以加入一行
from index import views
在下面相应的位置加入一行
url(r'^$', views.index, name = 'home'),
url括号内对应的参数含义:
r'^$'为正则表达式,表示匹配一个空的链接末(也就是说链接尾部什么都没有的时候走这条url),views.index为刚才在index这个子应用中的views之中的函数index
___
Chapter Five:运行调试服务器看结果
--
在主目录shift+右键→在此处运行命令行
python manage.py runserver 8000
最后也可以不指定端口数
用浏览器访问指定的url,就可以在浏览器中看到艰苦朴素的Hello world啦。