Brian Gesiak
Brian Gesiak

Reputation: 6958

Django trans and url tags

I want to translate a paragraph containing a URL in a Django 1.3 application.

<p>
    First <a href="{% url edit-profile username=user.username %}">edit your profile</a>, please.
</p>

Depending on the language, the text surrounded by <a> tags will surely change. How can I allow translators to decide on the link placement? Wrapping the entire thing in a {% trans %} causes an error:

<p>{% trans "First <a href='{% url edit-profile username=user.username %}'>edit your profile</a>, please." %}</p>

The error thrown is TemplateSyntaxError: Searching for value. Unexpected end of string in column 64: trans "First <a href='{% url edit-profile username=user.username.

How should I go about doing this? Do I have to determine the URL in the view, then pass that URL as a string to the template? That seems like a really convoluted solution for what I would think is a very common problem.

Upvotes: 22

Views: 8669

Answers (2)

Ming
Ming

Reputation: 21

This works for me:

{% url "app-name:name-of-view" as the_url %}
{% blocktrans %}
This is a URL: {{ the_url }}
{% endblocktrans %}

Upvotes: 1

Ned Batchelder
Ned Batchelder

Reputation: 375474

Use {% blocktrans %}. The Django translation docs include this example:

{% url path.to.view arg arg2 as the_url %}
{% blocktrans %}
This is a URL: {{ the_url }}
{% endblocktrans %}

Upvotes: 55

Related Questions