【问题标题】:Create template tags from the current function从当前函数创建模板标签
【发布时间】: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


    【解决方案1】:

    现在我们已经看到了完全废话的答案,这是废话的答案:

    在您的yourapp/templatetags/yourapptags.py 模块中:

    from django import template
    register = template.Library()
    
    DATE_FORMATS = {
        "en":  "l, F j, Y",
        "fr": "l, j F Y"
    }
    
    DEFAULT_LANG = 'fr'
    
    @register.simple_tag
    def localdate(lang=DEFAULT_LANG):
        fmt = DATE_FORMATS.get(lang, DATE_FORMATS[DEFAULT_LANG])
        now = datetime.now()
        with translation.override(lang):
            return datefilter(now, fmt)
    

    然后在你的模板中:

    {% load "yourapptags" %}
    
    <p>EN : {% localdate 'en' %}</p>
    <p>FR : {% localdate 'fr' %}</p>
    

    请注意,这是为了在给定的语言环境中强制日期格式/本地化,无论当前的语言环境是什么,在这种情况下,这对我来说似乎很奇怪。

    如果您真正想要的只是显示根据当前访问者的区域设置格式化的当前日期(参见https://docs.djangoproject.com/en/1.10/topics/i18n/translation/#how-django-discovers-language-preference),您可以去掉lang 参数并改用translation.get_language()

    @register.simple_tag
    def localdate():
        lang = translation.get_language()
        fmt = DATE_FORMATS.get(lang, DATE_FORMATS[DEFAULT_LANG])
        now = datetime.now()
        return datefilter(now, fmt)
    

    还要注意 Django 提供的 l10n 格式文件已经有some date formats defined and that you can override them

    【讨论】:

    • 我在一个 Django 的模板中使用了一个模板标签,所以这个 writting&lt;p&gt;EN : {% localdate 'en' %}&lt;/p&gt; 不受欢迎。即使您的答案可能更好,因为它非常简短紧凑,我必须使用此文字{{ customer|tmpl_date_customerprofile:'French' }}。你有什么建议?
    • "{{ customer|tmpl_date_customerprofile:'French' }}" 不是模板标签,它是一个应用了过滤器的模板变量。而且我看不出您为什么“必须使用”一种语法而不是另一种语法的任何理由-但是,您没有为您的问题提供任何背景,对吗?
    • 如果你理解的很好,Django的模板和html模板有点不同。两分钟后我会在我的问题中添加图片
    • 其实有一部分是在html中,另一部分是在非html中……奇怪!
    • 是的,在等待完整的测试套件运行时,我不应该在这里回答;)
    猜你喜欢
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 2013-01-26
    • 2011-06-27
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多