日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

Django路由系统

發布時間:2024/1/23 windows 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Django路由系统 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、路由系統基本格式
urlpatterns = [
path( 要匹配的路徑(可以是正則表達式), 視圖函數, 參數, 別名)
2、參數說明
(1) 正則表達式:一個正則表達式字符串
(2) 視圖函數:一個可調用對象,通常為一個視圖函數或一個指定視圖函數路徑的字符串
(3) ?參數:要傳遞給視圖函數的默認參數(字典形式,可選)
(4) ?別名:一個可選的name參數
3、正則表達式詳解
(1) 在python中使用 re_path模塊來寫正則表達式
(2) 正則表達式的開始使用“^”表示。
(3) 正則表達式的結束使用“$”表示。
(4) “r” 元字符串 防止正則表達式中的轉義。
urls.py

"""day03Django URL ConfigurationThe `urlpatterns` list routes URLs to views. For more information please see:https://docs.djangoproject.com/en/2.1/topics/http/urls/ Examples: Function views1. Add an import: from my_app import views2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views1. Add an import: from other_app.views import Home2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf1. Import the include() function: from django.urls import include, path2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from day03Django import views from django.urls import path,re_path urlpatterns = [path('admin/', admin.site.urls), path('laowang1/', views.laowang1), path('laowang2/', views.laowang2), path('laowang3/', views.laowang3), re_path(r'^laowang4/$',views.laowang4), re_path(r'^$',views.laowang5),#匹配一個空路徑 例如:http://127.0.0.1:8000/ re_path(r'^laowang6',views.laowang6),#不設置結尾,可以匹配多個路徑 如http://127.0.0.1:8000/laowang6/tianzhu/le re_path(r'^[a-zA-Z][a-zA-Z][0-9]',views.laowang7),#正則表達式 re_path(r'^laowang8/(\d+)/$',views.laowang8), re_path(r'^laowang9/(?P<year>[0-9]{4})/$',views.laowang9),#?必須是英文問號 re_path(r'^laowang10/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$',views.laowang10), re_path(r'^laowang11/(?P<year>[0-9]{4})/$',views.laowang11),#記住寫括號 re_path(r'^laowang12/(?P<month>[0-9]{2})/$',views.laowang12,{'year':'2019'}),#記住寫括號#在網站內不能修改re_path(r'^laowang13/$',views.laowang13),#記住寫括號 re_path(r'^laowang14/$',views.laowang14,name='lw14'),#記住寫括號 re_path(r'^laowang15/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$',views.laowang15,name='lw15'), #\d表示任意數字,+代表至少初夏一次 #上下移動代碼:shift+ctrl+上下鍵 #ctrl+d復制最后一行或選中的內容 #ctrl+z撤銷 ]

views.py

from django.shortcuts import HttpResponse,render def laowang1(request):return render(request,'laowang1.html',{'name':'天主極樂大帝'}) def laowang2(request):return render(request,'laowang2.html',{'name':'天主極樂大帝'}) def laowang3(request):return render(request,'laowang3.html',{'name':'天主極樂大帝'}) def laowang4(request):return render(request,'laowang4.html',{'name':'天主極樂大帝'}) def laowang5(request):return render(request,'laowang5.html') def laowang6(request):return render(request,'laowang6.html') def laowang7(request):return render(request,'laowang7.html') def laowang8(request,num):""":param request::param num::return:"""print(num)print(type(num))return render(request,'laowang8.html') def laowang9(request,year):print(year)return render(request,'laowang9.html') def laowang10(request,month,year):print(year,month)return render(request,'laowang10.html') def laowang11(request,year='2019'):#默認參數必須是字符串類型print(year)return render(request,'laowang11.html',{'year':year}) def laowang12(request,month,year):print(year,month)return render(request,'laowang12.html',{'year':year,'month':month}) def laowang13(request):return render(request,'laowang13.html') def laowang14(request):return render(request,'laowang14.html') def laowang15(request,month,year):print(type(month))return render(request,'laowang15.html',{'year':year,'month':month})

laowang13.html

from django.shortcuts import HttpResponse,render def laowang1(request):return render(request,'laowang1.html',{'name':'天主極樂大帝'}) def laowang2(request):return render(request,'laowang2.html',{'name':'天主極樂大帝'}) def laowang3(request):return render(request,'laowang3.html',{'name':'天主極樂大帝'}) def laowang4(request):return render(request,'laowang4.html',{'name':'天主極樂大帝'}) def laowang5(request):return render(request,'laowang5.html') def laowang6(request):return render(request,'laowang6.html') def laowang7(request):return render(request,'laowang7.html') def laowang8(request,num):""":param request::param num::return:"""print(num)print(type(num))return render(request,'laowang8.html') def laowang9(request,year):print(year)return render(request,'laowang9.html') def laowang10(request,month,year):print(year,month)return render(request,'laowang10.html') def laowang11(request,year='2019'):#默認參數必須是字符串類型print(year)return render(request,'laowang11.html',{'year':year}) def laowang12(request,month,year):print(year,month)return render(request,'laowang12.html',{'year':year,'month':month}) def laowang13(request):return render(request,'laowang13.html') def laowang14(request):return render(request,'laowang14.html') def laowang15(request,month,year):print(type(month))return render(request,'laowang15.html',{'year':year,'month':month})

laowang14.html

from django.shortcuts import HttpResponse,render def laowang1(request):return render(request,'laowang1.html',{'name':'天主極樂大帝'}) def laowang2(request):return render(request,'laowang2.html',{'name':'天主極樂大帝'}) def laowang3(request):return render(request,'laowang3.html',{'name':'天主極樂大帝'}) def laowang4(request):return render(request,'laowang4.html',{'name':'天主極樂大帝'}) def laowang5(request):return render(request,'laowang5.html') def laowang6(request):return render(request,'laowang6.html') def laowang7(request):return render(request,'laowang7.html') def laowang8(request,num):""":param request::param num::return:"""print(num)print(type(num))return render(request,'laowang8.html') def laowang9(request,year):print(year)return render(request,'laowang9.html') def laowang10(request,month,year):print(year,month)return render(request,'laowang10.html') def laowang11(request,year='2019'):#默認參數必須是字符串類型print(year)return render(request,'laowang11.html',{'year':year}) def laowang12(request,month,year):print(year,month)return render(request,'laowang12.html',{'year':year,'month':month}) def laowang13(request):return render(request,'laowang13.html') def laowang14(request):return render(request,'laowang14.html') def laowang15(request,month,year):print(type(month))return render(request,'laowang15.html',{'year':year,'month':month})

laowang15.html

<!DOCTYPE html> {# 加載static標簽 #} {#{% load static %}#} {#{% load static %}{# load負載,裝載量 #} <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body>老王15--> {{ year }}-->{{ month }} <a href="/laowang13/">調轉到13</a></body> </html>

總結

以上是生活随笔為你收集整理的Django路由系统的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。