imharjyotbagga
imharjyotbagga

Reputation: 329

'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)?

Upvotes: 0

Views: 453

Answers (1)

AzyCrw4282
AzyCrw4282

Reputation: 7754

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)) }}

Upvotes: 1

Related Questions