django blocktrans外部网址

时间:2015-04-12 15:14:42

标签: python django django-templates

我正在使用django框架构建一个Web应用程序。在我的一个模板中,我想添加一个指向外部网址的超链接。

在这个模板中,我需要有两种语言,所以我决定使用django blocktrans标签。从the django documentation开始,我只能将内部网址作为网址。 CMIIW。

如何设置外部网址?

例如。这是英语语言的template.html。我把http://external/docs/en/作为超链接

{% blocktrans %}
Hello, how are you? <a href="http://external/docs/en/" target="new">Lorem ipsum</a>. The quick brown fox jumps over the lazy dog.
{% endblocktrans %}

对于德语,我需要将http://external/docs/de/作为超链接

{% blocktrans %}
Hello, how are you? <a href="http://external/docs/de/" target="new">Lorem ipsum</a>. The quick brown fox jumps over the lazy dog.
{% endblocktrans %}

虽然django docs上的示例仅适用于内部URL。

{% if LANGUAGE_CODE == 'en' %}
  {% url 'views.doc.en' as urldoc %}
{% else %}
  {% url 'views.doc.de' as urldoc %}
{% endif %}
{% blocktrans %}
Hello, how are you? <a href={{ urldoc }} target="new">Lorem ipsum</a>. The quick brown fox jumps over the lazy dog.
{% endblocktrans %}

2 个答案:

答案 0 :(得分:0)

您可以直接在模板中使用{{ LANGUAGE_CODE }}的语言代码,并将其添加到网址中。例如:

{% blocktrans %}
Hello, how are you? <a href=http://external/docs/{{ LANGUAGE_CODE }}/ target="new">Lorem ipsum</a>. The quick brown fox jumps over the lazy dog.
{% endblocktrans %}

如果用户选择了英语,则{{ LANGUAGE_CODE }}将为en。如果用户选择了德语,则{{ LANGUAGE_CODE }}将为de

因此,链接将是:

答案 1 :(得分:0)

到目前为止,我的解决方案是views.py处的创建功能,该功能将重定向到外部网址。

@login_required
def doc_en():
    return HttpResponseRedirect('http://external/docs/en')

@login_required
def doc_de():
    return HttpResponseRedirect('http://external/docs/de')