1. 程式人生 > >Django學習(url配置及參數獲取)

Django學習(url配置及參數獲取)

dex nic body 學習 正則 def 開發 通過 位置

Django 如何處理一個請求

當用戶通過瀏覽器發送一個請求給Django網站時,Django執行過程:

1.Django首先在配置文件setting.py中找到 :ROOT_URLCONF = ‘test2.urls‘ 作為根模塊

2.加載模塊,執行項目包下面的urls.py 文件中的urlpatterns

3.執行應用包下面的urls.py文件中的urlpatterns

4.遍歷整個列表,通過正則表達式會找到基於view的函數或者類

5.如果沒有匹配到,則會自動調用Django的錯誤頁面

項目開發中配置url

1.在根目錄下的urls文件配置include 引入應用 stu下的urls

from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
    url(r^admin/, admin.site.urls),
    url(r^student/,include(stu.urls))
]

2.在應用stu\urls.py中配置

#coding=utf-8

from django.conf.urls import url
import views
urlpatterns=[
    url(r^hello/$
,views.index_view) ]

3.在應用stu中的views.py文件中定義一個函數

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
def index_view(request):
    return HttpResponse(hello)

4.訪問瀏覽器:輸入127.0.0.1:8000/student/hello/

帶參數的URLConf

.位置傳參

#stu\urls.py
from
django.conf.urls import url import views urlpatterns=[ url(r^hello/(\d{4})/(\d{2}),views.index1_view) ]
#stu\views.py
def
index1_view(request,num1,num2): return HttpResponse(hello_%s_%s %(num1,num2))

.關鍵字傳參


from django.conf.urls import url
import views

urlpatterns=[
    url(r^hello1/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$,views.index2_view)
]
def index2_view(request,year,month,day):
    return HttpResponse(hello_%s_%s_%s %(year,month,day))

.額外傳參

from django.conf.urls import url
import views

urlpatterns=[
 
    url(r^hello2/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$, views.index3_view,{name:lisi}),
]
def index3_view(request,year,month,day,name):
    return HttpResponse(hello_%s_%s_%s_%s % (year, month, day,name))

逆向解析(防止硬編碼)

.模板中的超鏈接

通過127.0.0.1:8000/student/ 訪問視圖views中index_view函數 ,進入index.html文件

在index_html文件中,通過超鏈接去訪問 name=‘hello‘ 路由地址

獲取到視圖views中index4_view函數中所返回的內容

#coding=utf-8
from django.conf.urls import url
import views
urlpatterns=[
    url(r^$,views.index_view),
    #逆向
    url(r^hello4/(\d{2}), views.index4_view,name=hello)
]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
def index_view(request):
    return render(request,index.html)

def index4_view(request,num):
    return HttpResponse(index4_view_%s %num)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<a href="{% url ‘hello‘ 13%}">鏈接1</a>
</body>
</html>

.視圖中的重定向

通過訪問127.0.0.1:8000/student/hello5/ 獲取到index5_view 函數

執行index5_view 通過重定向 獲取訪問name =‘hello‘ 的函數

進入index4_view 函數,通過傳入的參數 返回到頁面

urlpatterns=[
    url(r^$,views.index_view),
    #逆向
    url(r^hello4/(\d{2}), views.index4_view,name=hello),
    url(r^hello5/, views.index5_view)
]
def index4_view(request, num):
    return HttpResponse(index4_view_%s % num)

def index5_view(request):
    return HttpResponseRedirect(reverse(hello,args=(99,)))

Django學習(url配置及參數獲取)