vorpyg
vorpyg

Reputation: 2525

Encode URL in Django template

I need to encode an URL created by the {% url %} template tag in order to pass it as an argument in an iframe src which generates a Facebook Like button.

What's an appropriate way to do this? The urlencode template filter doesn't seem to work here. My template code looks like this:

{% url blog_urls/blog_detail slug=p.slug year=p.published_on.year month=p.published_on.month day=p.published_on.day %}

Upvotes: 2

Views: 4605

Answers (2)

Grant Anderson
Grant Anderson

Reputation: 362

Additionally, you can always use the filter tag itself to apply filters to more complex tags, eg:

{% filter urlencode %}{% url blog_urls/blog_detail slug=p.slug year=p.published_on.year month=p.published_on.month day=p.published_on.day %}{% endfilter %}

See https://docs.djangoproject.com/en/dev/ref/templates/builtins/#filter

Upvotes: 1

knutin
knutin

Reputation: 5133

The url tag takes another argument which allows you to create a variable with the value of the url:

{% url foo bar=baz as my_url %}
{{ my_url|filters }}}

Upvotes: 7

Related Questions