一、Django的安装:
1、python虚拟运行的环境的安装以及安装django:
1 sudo pip install virtualenv2 export VIRTUALENV_DISTRINUTR=true3 virtualenv envname4 source envname/bin/activate5 sudo pip install django==1.86 #deactivate
2、启动第一个项目:
1 django-admin.py startproject hellodjango2 cd hellodjango3 python manage.py runserver 0.0.0.0:8000
二、项目结构:
1、典型例子:
1 ├── djangolicious 2 │ ├── apps 3 │ │ ├── blog 4 │ │ │ ├── __init__.py 5 │ │ │ ├── models.py 6 │ │ │ ├── tests.py 7 │ │ │ └── views.py 8 │ │ ├── __init__.py 9 │ │ ├── news10 │ │ │ ├── __init__.py11 │ │ │ ├── models.py12 │ │ │ ├── tests.py13 │ │ │ └── views.py14 │ │ └── reader15 │ │ ├── __init__.py16 │ │ ├── models.py17 │ │ ├── tests.py18 │ │ └── views.py19 │ ├── __init__.py20 │ ├── libs21 │ │ ├── display22 │ │ │ ├── __init__.py23 │ │ │ ├── models.py24 │ │ │ ├── tests.py25 │ │ │ └── views.py26 │ │ ├── __init__.py27 │ │ └── management28 │ │ ├── __init__.py29 │ │ ├── models.py30 │ │ ├── tests.py31 │ │ └── views.py32 │ ├── settings.py33 │ ├── urls.py34 │ └── wsgi.py35 ├── manage.py36 ├── requirements37 │ ├── common.txt38 │ ├── dev.txt39 │ ├── prod.txt40 │ └── test.txt41 └── requirements.txt42 9个目录,32个文件
解释manage.py是一个启动管理脚本
models.py和数据看相关的代码
views.py视图函数
urls.py路由函数
wsgi.py python的http服务器
settings.py配置文件
doc文件夹:文档
requirements.txt包依赖说明文件
utils自定义包文件库文件
templates模板文件夹
static静态文件夹
三、第一个项目打印判断输入:
1、views.py
1 from django.http import HttpResponse 2 3 def authlogin(request): 4 request.encoding = "utf-8" 5 if "username" in request.GET: 6 username = request.GET.get("username") 7 else: 8 return HttpResponse("Login Name?") 9 if "password" in request.GET:10 password = request.GET.get("password")11 if password == "admin":12 return HttpResponse("Welcome %s"%username)13 else:14 return HttpResponse("Login Password Error!")15 else:16 return HttpResponse("Login Password?")
2、urls.py
1 from django.conf.urls import include, url 2 from django.contrib import admin 3 from . import views 4 5 urlpatterns = [ 6 # Examples: 7 # url(r'^$', 'login_welcome.views.home', name='home'), 8 # url(r'^blog/', include('blog.urls')), 9 10 url(r'^admin/', include(admin.site.urls)),11 url(r'^test/',views.authlogin)12 ]
然后启动即可
1 python manage.py runserver 0.0.0.0:8000