django之创建第7-6-第三种传值方式

1、创建bar.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>time</title>
</head>
<body>
<li>1、当前时间:{{today}}</li><br>
<li>my id:{{id}}</li>
<li>my name:{{name}}</li>
</body>
</html>

 

2、修改urls.py文件

# -*- coding: UTF-8 -*-
from django.conf.urls import patterns, include, url


urlpatterns = patterns('blog.views',
    url(r'^index/$', 'index'),
    url(r'^time/$', 'time'),
    url(r'^shengfen/$', 'shengfen'),
    #url(r'^foo/(\d{4})/$', 'foo'), #表示接收4个数字,#\w+接收至少一个字母
    url(r'^foo/(\d{4})/(\w+)/$',"foo"),
    url(r'^bar/(?P<id>\d{4})/(?P<name>\w+)/$','bar'),#强制规定bar(request,id,name)处的参数必须为id和name
)

 

3、修改views文件

def bar(request,id,name):
    t = loader.get_template("bar.html")
    user = {"today": datetime.datetime.now(),"id":id,"name":name}
    c = Context(user)
    return HttpResponse(t.render(c))

 

4、http://127.0.0.1:8000/blog/bar/1111/x/

 

5、百度云盘:django之创建第7-6-第三种传值方式

posted @ 2016-04-05 03:18  Xiao|Deng  阅读(162)  评论(0编辑  收藏  举报