【发布时间】:2017-03-30 13:59:03
【问题描述】:
我已经在我的models.py 文件中正式构造了函数:
from datetime import datetime
from django.template.defaultfilters import date as datefilter
from django.utils import translation
def date_customerprofile(language):
now_ = datetime.today()
if language == 'English':
translation.activate('en')
return datefilter(now_, 'l, F j, Y')
else:
translation.activate('fr')
return datefilter(now_, 'l, j F Y')
我知道 Django 的模板语言不是 Python,所以我们不能写 {{ customer.date_customerprofile('French') }}。
这里的解决方案是创建一个自定义模板标签。另请注意,translation.activate 将更改剩余请求/响应周期的活动语言 - 使用translation.override 上下文管理器肯定是一个更好的主意。由于我不是模板标签专家,任何人都可以告诉我如何在考虑到上述功能的情况下创建它?
我认为我们可以使用以下问题来做这样的事情How to format date in different languages?
我可以使用上下文管理器吗:Django switching, for a block of code, switch the language so translations are done in one language?
谢谢!
事实上,我想在模板中返回法语“Jeudi, 30 mars 2017”的当前日期或英语的当前日期Thursday, March 30, 2017。
P.S.如果问题不清楚,请告诉我。
这是 Django 模板的示例:
{% load i18n %}
{% load date %}
{{ customer.civil_title }}{{ user.get_full_name }}
{{ customer.civic_number }}, {{ customer.street }}
{{ customer.rough_location }}
{# customer. date_customerprofile('English') #}
7 DAYS NOTICE
Subject: Overdue account with ...
Client id : {{ customer.id }}
Hello {{ customer.civil_title }}{{ user.get_full_name }},
This notice serves to inform you that your payments with Gestion Multi Finance Inc. are pending. Your file has been transferred to our collection service. The balance of your loan is now: {{ customer.current_balance }}.
We are asking you to communicate with us without delay, by phone at: 1-855-... ext. 324 or by email at: ... to find a payment solution in order to regulate your situation. If this does not happen, we will be forced to send you a notice of acceleration of the process and transfer your file to our judiciary collection service, with authority to take the necessary steps in the collection process. These steps can include the seizure of salary, but we prefer to avoid these measures.
Also, it is important to remember that at the time of the loan, you signed a contract in which you agreed to respect the payments. Unfortunately, you have no respected this agreement.
Ignore this notice if an agreement has already been reached.
Cordially,
{{ email.footer.txt }}
【问题讨论】:
标签: python django templatetags