3

I'm using Google App Engine for Python with Jinja2 templating. Is there a template tag to get the URL of the current page. Or do I have to pass the url as a variable to the template from the view code?

David Xia
  • 5,075
  • 7
  • 35
  • 52
  • According to @mgilson, this can't be done. :( A [global](http://jinja.pocoo.org/docs/dev/api/#jinja2.Template.globals) function seems like the best alternative. – David Xia Sep 26 '14 at 15:14

1 Answers1

12

{{ request.url }} gives the current url. Do you need the full url, or just the path relative to the root?

For example, using Flask, you can have

{{ request.url }}
{{ request.base_url }}
{{ request.url_root }}
{{ request.host_url }}
{{ request.path }}

etc.

GAEfan
  • 11,244
  • 2
  • 17
  • 33
  • 1
    But you'd have to pass `request` to `template.render` -- I think OP wants it to just be there which (I think) is non-trivial at best ... -- Possibly a [`global`](http://jinja.pocoo.org/docs/dev/api/#jinja2.Template.globals) function which uses `webapp2.get_request().url`? – mgilson Sep 23 '14 at 03:28
  • What good is a template if you don't render it? This should be in the `context` params passed when rendering. – GAEfan Sep 23 '14 at 03:31
  • Not saying that I disagree -- And this it should be easy enough to always pass it via a particular function in a base `webapp2.RequestHandler` -- I was merely pointing out that it this answer doesn't quite fit the bill. – mgilson Sep 23 '14 at 03:32
  • Self has all that information http://stackoverflow.com/questions/2764586/get-current-url-in-python – Ryan Sep 23 '14 at 17:37