使用“现在”在 django blocktrans 中?

发布于 2024-10-16 16:24:52 字数 309 浏览 2 评论 0原文

我想使用下面的语法将年份添加到 Django blocktrans 中。

{% blocktrans with now|date:"Y" as copydate %}
     © {{ copydate }} Company
{% endblocktrans %}

这与现有的 Django 票证 (http://code.djangoproject.com/ticket/3088) 类似,它显然现在应该可以工作,但我也无法开始工作。

在这两种情况下,标签都没有扩展,但块传输的其余部分渲染得很好。

I'd like to add the year into a Django blocktrans - using the syntax below.

{% blocktrans with now|date:"Y" as copydate %}
     © {{ copydate }} Company
{% endblocktrans %}

This is similar to this existing Django ticket (http://code.djangoproject.com/ticket/3088), which apparently should work now but I can't get to work either.

In both cases the tag is simply not expanded, but the rest of the blocktrans renders fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟花肆意 2024-10-23 16:24:53

now 标记将格式化日期作为与传递的格式匹配的字符串返回。 date 可能需要一个 datetime/date 对象。所以将它们链接在一起是行不通的。

我什至不确定您是否可以在 with 语句中使用 now 标记,但请尝试一下。

{% blocktrans with now "Y" as copydate %}

now 接受与 date 相同的格式字符串。如果这也不起作用,我最好的选择是只向模板传递 datetime.datetime.now() 结果,并使用它而不是 now

{% blocktrans with my_date|date:"Y" as copydate %}

The now tag returns a formatted date as string matching the format passed. date probably needs a datetime/date object. So chaining these together wouldn't work.

I'm not even sure if you can use the now tag in the with statement, but try this.

{% blocktrans with now "Y" as copydate %}

now accepts the same format string as date. If this doesn't work either, my best bet would be to just pass the template a datetime.datetime.now() result, and use that instead of now.

{% blocktrans with my_date|date:"Y" as copydate %}
洒一地阳光 2024-10-23 16:24:52

唯一的方法是在 python 中获取日期并按照 Reiner 的建议使用日期过滤器或定义您自己的模板标签。
您可以创建一些上下文处理器来在您的上下文中设置日期。

def my_date(request):
  import datetime
  return {'my_date':datetime.datetime.now()}

并将其添加到 settings.py

TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + (
      'the_package_of_my_tiny_function.my_date',
      )

在模板中使用它,如下所示:

   {% blocktrans with my_date|date:"Y" as copydate %}
      © {{ copydate }} Company
   {% endblocktrans %}

不要忘记在视图中将 RequestContext 作为 context_instance 传递

这里是示例。

The only way is to get your date in python and use date filter as Reiner proposes or define your own templatetag.
You can create a little context processors to set date in your context.

def my_date(request):
  import datetime
  return {'my_date':datetime.datetime.now()}

and add this in settings.py

TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + (
      'the_package_of_my_tiny_function.my_date',
      )

Use it in your templates like this:

   {% blocktrans with my_date|date:"Y" as copydate %}
      © {{ copydate }} Company
   {% endblocktrans %}

Don't forget to pass RequestContext as context_instance in your views

Here is the example.

離殇 2024-10-23 16:24:52

从 Django 1.8 开始,您现在可以使用 {% now 'Y' as copydate %} 语法,因此您应该能够执行

{% now 'Y' as copydate %}
{% blocktrans %}© {{ copydate }} Company{% endblocktrans %}

以下操作 : djangoproject.com/en/dev/ref/templates/builtins/#now" rel="noreferrer">https://docs.djangoproject.com/en/dev/ref/templates/builtins/#now

Starting from Django 1.8, you can now use the {% now 'Y' as copydate %} syntax, so you should be able to do:

{% now 'Y' as copydate %}
{% blocktrans %}© {{ copydate }} Company{% endblocktrans %}

Source: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#now

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文