使用“现在”在 django blocktrans 中?
我想使用下面的语法将年份添加到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
now
标记将格式化日期作为与传递的格式匹配的字符串返回。date
可能需要一个datetime/date
对象。所以将它们链接在一起是行不通的。我什至不确定您是否可以在 with 语句中使用
now
标记,但请尝试一下。now
接受与date
相同的格式字符串。如果这也不起作用,我最好的选择是只向模板传递datetime.datetime.now()
结果,并使用它而不是now
。The
now
tag returns a formatted date as string matching the format passed.date
probably needs adatetime/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.now
accepts the same format string asdate
. If this doesn't work either, my best bet would be to just pass the template adatetime.datetime.now()
result, and use that instead ofnow
.唯一的方法是在 python 中获取日期并按照 Reiner 的建议使用日期过滤器或定义您自己的模板标签。
您可以创建一些上下文处理器来在您的上下文中设置日期。
并将其添加到 settings.py
在模板中使用它,如下所示:
不要忘记在视图中将 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.
and add this in settings.py
Use it in your templates like this:
Don't forget to pass RequestContext as context_instance in your views
Here is the example.
从 Django 1.8 开始,您现在可以使用
{% now 'Y' as copydate %}
语法,因此您应该能够执行以下操作 : 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:Source: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#now