简体   繁体   中英

'url' tag for tornado (python) with parameters, just like in Django Template Language

Like in Django, we can use DTL(Django Template Language) {% url 'url_name' %} instead of hard coding URL names. Is anything of that sort is available while using Tornado (python)?

You can use {{ reverse_url('login') }} . That is, a template expression rather than directive. The syntax reference is here (it's brief).

For example,

To name the urls you need full URLSpec objects -- see here

In this particular example, you can easily use the helper tornado.web.url function:

from tornado.web import url

urls = [
    url(r"/", IndexHandler, name="home"),
]

And in template access it like so

<a class="navbar-brand navbar-right" href="{{reverse_url('home')}}">

Update: To pass with parameters, follow the method below.

Use reverse_url to construct the base url, and then add query parameters afterwards. Example from here

{{ reverse_url("web-html", "list-builds") + "?" + urlencode(dict(bundle_identifier=app.bundle_identifier)) }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM