django URL的補充 預設值 傳多個引數
阿新 • • 發佈:2018-12-08
url 後面還可以加上預設值
預設值
url(r'^index/', views.index, {'name': 'root'}),
urls.py url對應關係
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^index/', views.index), ]
預設情況是這樣
views.py 程式碼
from django.shortcuts import render from django.shortcuts import HttpResponse # Create your views here. def index(request): return HttpResponse("index")
沒有問題
傳遞引數
from django.conf.urls import url from django.contrib importadmin from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), # url(r'^index/', views.index), url(r'^index/', views.index,{'name':'root'}), ]
報錯了
因為在沒有傳遞形式引數給函式
需要傳預設值時候,必須再加上一個形式引數接受一下,而名字根據字典key來定的
相當於給函式或類傳一個值
defindex(request,name): print(name) return HttpResponse("index")
後臺拿到的
root [07/Dec/2018 15:52:14] "GET /index/ HTTP/1.1" 200 5