zoukankan      html  css  js  c++  java
  • Python笔记——Django路由系统

    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),
    ]
    urls

    from django.shortcuts import render
    from django.shortcuts import HttpResponse
    
    # Create your views here.
    
    def home(request):
        #return "asdf"
        return HttpResponse('OK')
    views

    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 ]
    oneurls.py
    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)
    app01views.py
    from django.conf.urls import url
    from django.contrib import admin
    from app01 import views
    
    urlpatterns = [
        url(r'^home/', views.home),
    ]
    app01urls.py
    1 from django.shortcuts import render,HttpResponse
    2 
    3 # Create your views here.
    4 
    5 def home(request):
    6     return HttpResponse('app02.home')
    app02views.py
    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 ]
    app02urls.py

  • 相关阅读:
    杭电 1013 Digital Roots
    杭电 1040 As Easy As A+B 【排序】
    杭电 2092 整数解
    bzoj3223
    bzoj3224
    LA3905
    bzoj3601
    bzoj1002
    bzoj3105
    bzoj3332
  • 原文地址:https://www.cnblogs.com/yard/p/6715028.html
Copyright © 2011-2022 走看看