1、创建Django程序
django-admin startproject mysite 创建project python manage.py startapp app01 创建app01 python manage.py startapp app02 创建app02
2、执行DJango程序
进入Project
python manage.py runserver 127.0.0.1:8000
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^home/', views.home), ]
from django.shortcuts import render from django.shortcuts import HttpResponse # Create your views here. def home(request): #return "asdf" return HttpResponse('OK')
python manage.py runserver 127.0.0.1:8000
执行结果
3、Django依赖数据库配置,settings
生成数据库表:
Python manage.py makemigratios # 生成配置文件 Python manage.py migrate # 根据配置文件创建数据库相关
4、admin
Python manage.py createsuperuser ... http://localhost:8000/admin/
admin主要作用:用来快速操作数据库
admin应用:点名系统、单元测试
5、路由系统
1、静态路由 2、动态路由 按照顺序,第n个匹配的数据,交给函数的第n个参数,严格按照顺序模板的方法,将匹配的参数,传给指定的形式参数 3、二级路由 app01 url.py project_name URL : app01 -> include("appo1.url)
1 from django.conf.urls import url,include 2 from django.contrib import admin 3 from app01 import views 4 5 urlpatterns = [ 6 url(r'^admin/', admin.site.urls), 7 url(r'^home/', views.home), 8 url(r'^news/(d+)/(d+)', views.news), # 传两个参数 9 url(r'^page/(?P<n1>d+)/(?P<n2>d+)', views.page), # 接收两个参数,不按顺序,指定形式参数 10 url(r'^app01/', include("app01.urls")), # 二级路由 11 url(r'^app02/', include("app02.urls")), 12 ]
from django.shortcuts import render from django.shortcuts import HttpResponse # Create your views here. def home(request): #return "asd" return HttpResponse('App01.home') def news(request,nid1,nid2): #return "asd" nid = nid1 + nid2 return HttpResponse(nid) def page(request,n1,n2): #return "asd" nid = n1 + n2 return HttpResponse(nid)
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^home/', views.home), ]
1 from django.shortcuts import render,HttpResponse 2 3 # Create your views here. 4 5 def home(request): 6 return HttpResponse('app02.home')
1 from django.conf.urls import url 2 from django.contrib import admin 3 from app02 import views 4 5 urlpatterns = [ 6 url(r'^home/', views.home), 7 ]