作者: limodou

<<TableOfContents>>
== 题目要求 ==

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

== 框架说明 ==

Uliweb (http://code.google.com/p/uliweb)

== 步骤 ==
在命令行下操作

=== 创建Project3项目 ===

{{{
uliweb makeproject project3
}}}

=== 创建Hello App ===

{{{
cd project3
uliweb makeproject Hello
}}}

=== 修改project3/apps/Hello/views.py ===

{{{
#!python
@expose('/')
def index():
    return {}

@expose('/test')
def test():
    return "test href link"
}}}

=== 在project3/apps/Hello/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_for('Hello.views.test')}}"> test_url_for</a>
</body>
</html>
}}}

=== 结束 ===

== 测试 ==
{{{
cd project3
uliweb runserver
}}}

访问 http://localhost:8000/

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

== 说明 ==

Uliweb中也有url_for函数,但是在启动uliweb服务时会自动添加到模板的运行环境中,不需要导入。