1. 程式人生 > >4.5、Django - URL之Django內建的URL轉換器

4.5、Django - URL之Django內建的URL轉換器

1、 轉化器

可以通過

from django.urls import converters

引入轉換器,或檢視對應的有哪些轉換器 。

2.1 整型int轉換器

控制引數型別,比如,控制出版社的id為整型 。

在douAPI專案APP為douBook的views.py中:

from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse

# def book_info(request):
#     return HttpResponse("圖書詳情")
# def book_info_detail(request,book_id): # # 從資料庫中拿book_id,省略 # your_book_id = 'your book id is :{book_id}'.format(book_id = book_id) # return HttpResponse(your_book_id) # 注意book_author_detail函式與book_info_detail(request,book_id)的區別,前者的除了request引數寫在括號內,其餘不寫,在函式體內部體現 # def book_author_detail(request):
# # 從資料庫中拿id # author_id = request.GET.get('id') # 或者author_id = request.GET['id'] # content = "Author's id of the book is : {input_id}".format(input_id = author_id) # return HttpResponse(content) def book_publisher_id(request,publisher_id): content = "Publisher's id of the book is : {input_id}"
.format(input_id = publisher_id) return HttpResponse(content)

在douAPI專案下的urls.py中:

from django.urls import path
from douBook import views

urlpatterns = [
    # path('douBook/', views.book_info),
    # # 注意:引數<book_id>必須與APP為douBook下的views.py中book_info_detail函式的引數book_id保持一致!!!
    # path('douBook/<book_id>/', views.book_info_detail),
    # 注意:通過查詢字串的形式傳參,即GET請求方法,不需要加<>,author_id部分也可以隨意寫,比如寫成Auth_id
    # path('douBook/author/', views.book_author_detail),
    path('douBook/<int:publisher_id>/', views.book_publisher_id),
]

當輸入整數時,結果如下:

在這裡插入圖片描述

當輸入非整數時,結果會報404錯誤:

在這裡插入圖片描述

2.2 str轉換器

除了/其餘都可以作為str型別,預設時str轉換器 。

2.3 uuid轉換器

uuid轉換器具有唯一性,只滿足uuid.uuid4()型別的 。

import uuid
print(uuid.uuid4())
# output:  d4d16dd7-5b81-42f8-9c2a-397c2e193ad3

在douAPI專案APP為douBook的views.py中:

from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse

# def book_info(request):
#     return HttpResponse("圖書詳情")

# def book_info_detail(request,book_id):
#     # 從資料庫中拿book_id,省略
#     your_book_id = 'your book id is :{book_id}'.format(book_id = book_id)
#     return HttpResponse(your_book_id)

# 注意book_author_detail函式與book_info_detail(request,book_id)的區別,前者的除了request引數寫在括號內,其餘不寫,在函式體內部體現
# def book_author_detail(request):
#     # 從資料庫中拿id
#     author_id = request.GET.get('id') # 或者author_id = request.GET['id']
#     content = "Author's id of the book is : {input_id}".format(input_id = author_id)
#     return HttpResponse(content)


def book_publisher_id(request,publisher_id):
    content = "Publisher's id of the book is : {input_id}".format(input_id = publisher_id)
    return HttpResponse(content)

在douAPI專案下的urls.py中:

from django.urls import path
from douBook import views

urlpatterns = [
    # path('douBook/', views.book_info),
    # # 注意:引數<book_id>必須與APP為douBook下的views.py中book_info_detail函式的引數book_id保持一致!!!
    # path('douBook/<book_id>/', views.book_info_detail),
    # 注意:通過查詢字串的形式傳參,即GET請求方法,不需要加<>,author_id部分也可以隨意寫,比如寫成Auth_id
    # path('douBook/author/', views.book_author_detail),
    # path('douBook/<int:publisher_id>/', views.book_publisher_id),
    path('douBook/<uuid:publisher_id>/', views.book_publisher_id),
]

當隨意輸入一個字串時,結果報404錯誤:

在這裡插入圖片描述

當將上面生成的uuid輸入時,結果如下:

在這裡插入圖片描述

2.4 slug轉換器

該轉換器滿足:regex = '[-a-zA-Z0-9]'
即:數字、大小寫字母、-滿足,其餘不行 。

2.5 path轉換器

上面談及str轉換器不能有/ 而path轉換器是可以包含任意字元的,包括/
在這裡插入圖片描述

2.6 自定義轉化器(省略)