django学习笔记(四)

url地址

learn/urls.py

from django.conf.urls import include, url
from django.contrib import admin
from blog.views import index

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^blog/index/$',index)
]
from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^blog/index/$','blog.views.index')
]

url传参

①关键字参数

learn/urls.py

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^blog/index/(?P<id>\d{2})/$','blog.views.index')
]

blog/views.py

def index(request,id):
    user={'name':'Tom','age':34,'sex':'male'}
    #user=Person('tom',23,'male')
    book_list = ['Python','java','php','web']
    return render_to_response('test1.html',{'title':'Hello','user':user,'book_list':book_list,'id':id})
②未命名参数

learn/urls.py

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^blog/index/(\d{2})/$','blog.views.index')
]

blog/views.py

def index(request,p):
    user={'name':'Tom','age':34,'sex':'male'}
    #user=Person('tom',23,'male')
    book_list = ['Python','java','php','web']
    return render_to_response('test1.html',{'title':'Hello','user':user,'book_list':book_list,'id':p})

推荐阅读更多精彩内容