Use 'now' in django blocktrans?

I would like to add a year to the Django block diagram - 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 work either.

In both cases, the tag simply does not expand, but the other block transceivers are displayed perfectly.

+6
django templates internationalization
source share
3 answers

The only way is to get your date in python and use a date filter, as Rainer offers or defines your own templatetag. You can create small context processors to set the date in your context.

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

and add this to settings.py

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

Use it in your templates as follows:

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

Remember to pass RequestContext as context_instance in your views

Here is an example.

+10
source share

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

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

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

+5
source share

The now tag returns a formatted date as a string corresponding to the transmitted format. date probably needs a datetime/date object. Thus, combining these elements will not work.

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

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

now accepts the same format string as date . If this does not work, the best option would be to simply pass the template a datetime.datetime.now() result and use it instead of now .

 {% blocktrans with my_date|date:"Y" as copydate %} 
+1
source share

All Articles