简体   繁体   English

如何在 django 模板中包含反向 url 翻译

[英]How to include in django template a reverse url in translation

I have the following block of code, And I want to include a url to the login page, after the user logs out, in a translatable text.我有以下代码块,并且我想在用户注销后以可翻译文本的形式将 url 包含到登录页面。

Unfortunately, translation blocks cannot include tags, and I get the following error:不幸的是,翻译块不能包含标签,我收到以下错误:

SyntaxError: Translation blocks must not include other block tags: url "account:login" SyntaxError:翻译块不得包含其他块标签:url "account:login"

{% blocktrans %}


You have been successfully logged out.


You can <a href="{% url "account:login" %}">log-in again</a>.


{% endblocktrans %}

urls.py:网址.py:

from django.urls import path

from django.contrib.auth import views as auth_views



app_name = 'account'



urlpatterns = [

    path('login/', auth_views.LoginView.as_view(), name='login'),

    path('logout/', auth_views.LogoutView.as_view(), name='logout'),
]

What would be the right way to achieve something like this?实现这样的目标的正确方法是什么?

Edit: I figured there are workarounds, such as translating blocks of text separately, or using javascript to append the "href" element after the page loads.编辑:我认为有一些解决方法,例如单独翻译文本块,或者在页面加载后使用 javascript 到 append 的“href”元素。 But I wonder if there is a more efficient, Django way.但我想知道是否有更高效的 Django 方式。

As documented记录的

Reverse URL lookups cannot be carried out within the blocktrans and should be retrieved (and stored) beforehand:无法在 blocktrans 中执行反向 URL 查找,应事先检索(和存储):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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