简体   繁体   中英

django blocktrans external url

I am building a web app using django framework. At one of my template, I'd like to put a hyperlink to external URL .

At this template, I need to have two languages, so I decided to use django blocktrans tag. From the django documentation ), I could only put internal URL as the URL. CMIIW.

How do I put my external URL?

eg. this is the template.html for english language. I put http://external/docs/en/ as the hyperlink

{% 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 %}

while for german language, I need to put http://external/docs/de/ as the hyperlink

{% 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 %}

While the example at the django docs is only for internal 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 %}

You can directly use the language code with {{ LANGUAGE_CODE }} in the template and add it to the url. For example:

{% 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 %}

When the user has English language selected {{ LANGUAGE_CODE }} will be en . If the user has German language selected {{ LANGUAGE_CODE }} will be de

Therefore, the links will be:

So far, my solution is create function at views.py which will redirect to external URL.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM