Differences between revisions 1 and 4 (spanning 3 versions)
Revision 1 as of 2009-10-12 02:49:11
Size: 1292
Editor: ubunoon
Comment:
Revision 4 as of 2009-12-25 07:14:25
Size: 1178
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
写一个template程序,实现url动态变换 url_for方式使用,在模板中动态实现url
Line 37: Line 37:
=== 4. 修改project2/lib/helpers.py ===

增加下面一条语句
{{{
from routes import url_for
}}}
Line 54: Line 47:
<a href="${h.url_for( controller='hello', action='test')}"> test_url_for</a> <a href="${url( controller='hello', action='test')}"> test_url_for</a>

题目要求

url_for方式使用,在模板中动态实现url

框架说明

Pylons (http://pylonshq.com)

步骤

在命令行下操作

1. 创建Project2项目

paster create -t pylons project2

2. 创建Hello App

cd project2
paster controller hello

3. 修改project2/controllers/hello.py

   1 def index(self):
   2     return render('/index.html')
   3 
   4 def test(self):
   5     return "test href link"

5. 在project2/templates下创建index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title>Hello World</title>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
</head>
<body>
<a href="${url( controller='hello', action='test')}"> test_url_for</a>
</body>
</html>

6. 结束

测试

cd project2
paster serve --reload development.ini

访问 http://localhost:5000/hello/index

进入连接在超链接上放置鼠标,可以在浏览器底部看到test_url_for所指向的连接地址: http://localhost:5000/hello/test

PylonsUrlFor (last edited 2009-12-25 07:14:25 by localhost)