django routing layer (a)

Django's routing system


Django 1.11 version URLConf official documents

URL configuration (URLconf) as the Django directory site support. Its essence is a mapping table between the URL and the URL you want to call that view function.

In this way we are told Django, which encountered URL of time, to which the corresponding function is executed.


URLconf configuration


The basic format:


Example:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^test', views.test),
    url(r'^testadd',views.testadd)
]

django project directory, routing layer is urls.py this file to be controlled

Urlpatterns a list of variable definitions, distributed to the route list view function layer url

url function is matched by the last regular browser, enter the URL in the path were regular match (match admin, test, testadd)


Parameter Description:

  • Regular Expressions: A regular expression string
  • views view: a callable object, a view is generally a function of
  • Parameters: The optional parameters to be passed to the default view function (a dictionary)
  • Alias: An optional parameter name


Code verification

urls.py

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url('test', views.test),
    url('testadd',views.testadd)
]

views.py

def test(request):
    return HttpResponse('test')

def testadd(request):
    return HttpResponse('testadd')

浏览器测试

Enter http://127.0.0.1:8000/test/ , return test

Enter http://127.0.0.1:8000/testadd/ , also returned test


demand analysis

The above results are not what we want, we want the result is:

Enter http://127.0.0.1:8000/testadd/, page return testadd

Enter http://127.0.0.1:8000/test/, return test page


From the above results and verification know:

The first parameter routing file follow behind url function is a regular expression matching rules down in accordance with a match from the match to a match immediately after.

When we enter http://127.0.0.1:8000/testadd/ , url ( 'the Test', views.test), which matches a first element to testadd, equivalent re.findall ( 'test', 'testall ') , thus matching to the test string, executes the corresponding view of the test function layer, the final back to test

>>> import re
>>> re.findall('test','testall')
['test']


Different input requirements of different return path url string


Solution 1:

urls.py

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url('test/', views.test),
    url('testadd/',views.testadd)
]

浏览器测试

Enter http://127.0.0.1:8000/test/ , return test

Enter http://127.0.0.1:8000/testadd/, page return testadd

But there is a problem:

When we enter http://127.0.0.1:8000/test/sdsddsdsfsdf , normal return test

When we enter http://127.0.0.1:8000/dsdsdsdssdtest/sdsddsdsfsdf , normal return test

When we enter http://127.0.0.1:8000/testadd/sdsddsdsfsdf , normal return testadd

When we enter http://127.0.0.1:8000/dsdsdsdssdtestadd/sdsddsdsfsdf , normal return testadd


Solution 2:

Because the route matching is the law of the regular matches, so use regular expressions to match precisely

urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url('^test/$', views.test),
    url('^testadd/$',views.testadd)
]


2 Home also hope demand content, users mistype a URL to a message

urls.py

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$',views.home),
    url('^test/$', views.test),
    url('^testadd/$',views.testadd),
    url(r'', views.error),
]

views.py

from django.shortcuts import render,redirect,HttpResponse

# Create your views here.


def home(request):
    return HttpResponse('这是首页')

def error(request):
    return HttpResponse('你输了什么鬼')

def test(request):
    return HttpResponse('test')

def testadd(request):
    return HttpResponse('testadd')

浏览器测试


Route distribution


url('正则表达式','视图函数内存地址')

django每一个app下面都可以有自己的url.py 路由层,templates文件夹,static文件夹

项目名下urls.py(总路由)不再做路由与视图函数的匹配关系而是做路由的分发

from django.conf.urls import include

# 路由分发  注意路由分发总路由千万不要$结尾
url(r'^app01/',include(app01_urls)),
url(r'^app02/',include(app02_urls))

# 在应用下新建urls.py文件,在该文件内写路由与视图函数的对应关系即可
from django.conf.urls import url
from app01 import views
urlpatterns = [
    url(r'^index/',views.index)
]


案例

urls.py

from django.conf.urls import url, include


urlpatterns = [
    url(r'app01/',include('app01.urls')),
    url(r'app02/',include('app02.urls')),
]

app01.urls

from django.conf.urls import url
from app01 import views

urlpatterns = [
   url('^index/',views.index)
]

app01.views

def index1(request):
    return HttpResponse('index1')

app02.urls

from django.shortcuts import render,HttpResponse

# Create your views here.

def index(request):
    return HttpResponse('index2')

app02.views

def index2(request):
    return HttpResponse('index2')


分组


无名分组

将加括号的正则表达式匹配到的内容当做位置参数自动传递给对应的视图函数

默认匹配到后,默认把request一定会传给视图函数,因为url函数是采取正则匹配,正则匹配中如果匹配到分组的内容,则输出分组,所以我们可以通过正则表达式分组的特点将匹配到的分组的内容传递给视图函数,但是视图函数需要增加一个形参来接收正则匹配匹配到的分组的内容


>>> re.findall('test/([0-9]+)','test/2018')
['2018']
>>> re.findall('test/([0-9]+)','test/20183333333')
['20183333333']
url(r'^test/(\d+)/', views.test),  # 匹配一个或多个数字
def test(request,xxx):
    print(xxx)
    return HttpResponse('test')

url.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.home),
    # url(r'^test/[0-9]{4}/', views.test),
    url(r'^test/(\d+)/', views.test),  # 匹配一个或多个数字
    url(r'^testadd/', views.testadd),
    # url(r'',views.error)
]

views.py

from django.shortcuts import render,HttpResponse,redirect


def test(request):
    return HttpResponse('test')

def testadd(request):
    return HttpResponse('testadd')

def home(request):
    return HttpResponse('首页')

def error(request):
    return HttpResponse('广告位招租')

显示的页面

修改代码

views.py

from django.shortcuts import render,HttpResponse,redirect


def test(request,xxx):
    print(xxx)
    return HttpResponse('test')


有名分组

将加括号的正则表达式的内容当做关键字参数自动传递给响应的视图函数

正则表达式复习

>>> a=re.match('test/(?P<name>[0-9]+)','test/123')
>>> a.group('name')
'123'

>>> a=re.match('test/(?P<name>\d+)','test/123')
>>> a.group('name')
'123'

使用

url(r'^test/(?P \d+)/', views.test)

urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.home),
    # url(r'^test/[0-9]{4}/', views.test),
    # 无名分组
    # url(r'^test/(\d+)/', views.test),  # 匹配一个或多个数字

    # 有名分组
    url(r'^test/(?P<year>\d+)/', views.test),  # 匹配一个或多个数字,并可以通过year取值
    url(r'^testadd/', views.testadd),
    # url(r'',views.error)

views.py

def test(request,year): # 这里的形参year必须和路由层中正则匹配的名字是一样的
    print(year)
    return HttpResponse('test')


注意


有名分组和无名分组不能混着用,但是多个有名分组和多个无名分组可以连用


代码验证

同时使用无名分组和有名分组, 报错

urls.py

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    # 测试 同时使用无名分组和有名分组
    url(r'test/(?P<year>\d+)/[0-9](\d+)',views.test),
]

views.py

def test(request,xxx,year):
    print(year)
    return HttpResponse('test')

使用多个有名分组

urls.py

urlpatterns = [
    # 测试 使用多个无名分组
    url(r'test/(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)',views.test),
]

views.py

from django.shortcuts import render,HttpResponse,redirect


def test(request,month,day,year):
    print(year)
    return HttpResponse('test')

Guess you like

Origin www.cnblogs.com/cjwnb/p/11788511.html