![](/img/trans.png)
[英]How to Include JavaScript variable in Django URL in template?
[英]How to include in django template a reverse url in translation
我有以下代碼塊,並且我想在用戶注銷后以可翻譯文本的形式將 url 包含到登錄頁面。
不幸的是,翻譯塊不能包含標簽,我收到以下錯誤:
SyntaxError:翻譯塊不得包含其他塊標簽:url "account:login"
{% blocktrans %}
You have been successfully logged out.
You can <a href="{% url "account:login" %}">log-in again</a>.
{% endblocktrans %}
網址.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'),
]
實現這樣的目標的正確方法是什么?
編輯:我認為有一些解決方法,例如單獨翻譯文本塊,或者在頁面加載后使用 javascript 到 append 的“href”元素。 但我想知道是否有更高效的 Django 方式。
如記錄的
無法在 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.