1. 程式人生 > >python測試開發django-4.獲取url引數和name的作用

python測試開發django-4.獲取url引數和name的作用

前言

如開啟部落格園按時間分類標籤頁【https://www.cnblogs.com/yoyoketang/archive/2018/10.html】,裡面是時間引數是動態的,如果我想獲取裡面的時間引數2018和10這兩個引數,這就涉及到url引數的獲取了。

獲取url引數

先用path去匹配一個url地址,類似於:archive/2018/10.html,於是取兩個引數名稱year,month。引數用<name>這種格式

from django.conf.urls import url
from django.urls import re_path, path
from hello import views
urlpatterns = [

    # 匹配 archive/2018/10.html
    path("archive/<year>/<month>.html", views.home),
]

hello.py/views.py檢視函式內容

from django.shortcuts import render
from django.http import HttpResponse, Http404

# Create your views here.

def home(request, year="2018", month="01"):
    return HttpResponse("獲取當前頁面home時間標籤:%s年/%s月" %(year, month))

啟動服務後,瀏覽器輸入地址:http://127.0.0.1:8000/archive/2018/10.html

正則匹配url

上面的案例雖然可以實現從url上獲取引數了,但是會遇到一個問題,年和月可以輸入各種資料,如:archive/2018/101.html,很顯然不太合理。
如果想讓year引數只能是4個數字,month引數只能是2個數字,該怎麼做呢?這就需要用到正則匹配了。

  • ?P 引數year
  • [0-9] 匹配0-9的數字
  • {4} 匹配4個數字
  • {1,2} 匹配1-2個數字
  • r 是raw原型,不轉義
  • ^ 匹配開始
  • $ 匹配結束
from django.conf.urls import url
from django.urls import re_path, path
from hello import views
urlpatterns = [

    # 匹配 archive/2018/10.html
    path("archive/<year>/<month>.html", views.home),
    url(r'^archive1/(?P<year>[0-9]{4})/(?P<month>[0-9]{1,2}).html$', views.home1)
]

hello.py/views.py檢視函式內容

from django.shortcuts import render
from django.http import HttpResponse, Http404

# Create your views here.

def home(request, year="2018", month="01"):
    return HttpResponse("獲取當前頁面home時間標籤:%s年/%s月" %(year, month))

def home1(request, year="2018", month="01"):
    return HttpResponse("獲取當前頁面home1時間標籤:%s年/%s月" %(year, month))

啟動服務後,瀏覽器輸入地址:http://127.0.0.1:8000/archive1/2018/10.html
(month輸入一位數字也可以如:http://127.0.0.1:8000/archive1/2018/1.html)

urls.py中定義name的作用

如果現在有一個home.html頁面,還有一個demo.html頁面,之前兩個頁面是獨立的不相干的,如果現在需要從home頁,點個按鈕,跳轉到demo.html該如何實現?

hello/templates/home.html寫入以下內容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>上海-悠悠</title>
    <base href="http://127.0.0.1:8000/" target="_blank">
</head>
<body>
<p>歡迎來到django!
    <br>
    <br>
    <a href="demo/">點這裡到demo頁</a>
</p>
</body>
</html>

hello/templates/demo.html寫入以下內容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>demo樣式</title>
</head>
<body>

<p>
    <h4> 這是我的部落格地址,可以百度搜:上海-悠悠 </h4>
    <a href="https://www.cnblogs.com/yoyoketang/" target="_blank" >上海-悠悠-部落格園</a>
    <hr>
    <h4> 《python自動化框架pytest》 </h4>
    <p>pytest是最強大最好用的python自動化框架,沒有之一。本書詳細講解pytest框架使用方法,fixture功能是pytest的精髓,書中有詳細的案例講解。<br>
        另外最後會有專案實戰程式碼,靈活用到selenium自動化專案上。<br>
        pytest交流群874033608

    </p>
    <a href="https://yuedu.baidu.com/ebook/902224ab27fff705cc1755270722192e4536582b" target="_blank" >百度閱讀地址點此</a>
</p>

</body>
</html>

hello/views.py檔案

from django.shortcuts import render
from django.http import HttpResponse, Http404

# Create your views here.
def home(request):
    return render(request, 'home.html')

def demo(request):
    return render(request, 'demo.html')

helloworld/urls.py檔案內容


from django.conf.urls import url
from django.urls import re_path, path
from hello import views
urlpatterns = [
    url('^demo/$', views.demo),
    url('^home/', views.home),
]

這樣就可以實現在home頁點點這裡到demo頁

如果在頁面裡面把url地址寫死了:<a href="demo/">點這裡到demo頁</a>,這樣會有個弊端,當多個頁面用到這個地址時候,如果後續這個地址變了,那就很難維護了。
為了url地址維護起來方便,可以給它去個唯一的名稱,也就是name引數,接下來在url配置里加個name名稱。

from django.conf.urls import url
from django.urls import re_path, path
from hello import views
urlpatterns = [
    url('^demo/$', views.demo,  name="demo_page"),
    url('^home/', views.home,  name="home_page"),
]

把hello/templates/home.html跳轉的地址改成如下:

<a href="{% url 'demo_page' %}">跳轉到demo頁面</a>

django更多關於urls學習可以參考【https://docs.djangoproject.com/zh-hans/2.0/topics/http/urls/】
python學習交流QQ群:588402570