Fixed #23914 -- Improved {% now %} to allow storing its result in the context.
Thanks to Tim for the review.
This commit is contained in:
parent
3a9aa155e2
commit
c335c0fee9
|
@ -419,12 +419,19 @@ class LoadNode(Node):
|
||||||
|
|
||||||
|
|
||||||
class NowNode(Node):
|
class NowNode(Node):
|
||||||
def __init__(self, format_string):
|
def __init__(self, format_string, asvar=None):
|
||||||
self.format_string = format_string
|
self.format_string = format_string
|
||||||
|
self.asvar = asvar
|
||||||
|
|
||||||
def render(self, context):
|
def render(self, context):
|
||||||
tzinfo = timezone.get_current_timezone() if settings.USE_TZ else None
|
tzinfo = timezone.get_current_timezone() if settings.USE_TZ else None
|
||||||
return date(datetime.now(tz=tzinfo), self.format_string)
|
formatted = date(datetime.now(tz=tzinfo), self.format_string)
|
||||||
|
|
||||||
|
if self.asvar:
|
||||||
|
context[self.asvar] = formatted
|
||||||
|
return ''
|
||||||
|
else:
|
||||||
|
return formatted
|
||||||
|
|
||||||
|
|
||||||
class SpacelessNode(Node):
|
class SpacelessNode(Node):
|
||||||
|
@ -1200,10 +1207,14 @@ def now(parser, token):
|
||||||
It is {% now "jS F Y H:i" %}
|
It is {% now "jS F Y H:i" %}
|
||||||
"""
|
"""
|
||||||
bits = token.split_contents()
|
bits = token.split_contents()
|
||||||
|
asvar = None
|
||||||
|
if len(bits) == 4 and bits[-2] == 'as':
|
||||||
|
asvar = bits[-1]
|
||||||
|
bits = bits[:-2]
|
||||||
if len(bits) != 2:
|
if len(bits) != 2:
|
||||||
raise TemplateSyntaxError("'now' statement takes one argument")
|
raise TemplateSyntaxError("'now' statement takes one argument")
|
||||||
format_string = bits[1][1:-1]
|
format_string = bits[1][1:-1]
|
||||||
return NowNode(format_string)
|
return NowNode(format_string, asvar)
|
||||||
|
|
||||||
|
|
||||||
@register.tag
|
@register.tag
|
||||||
|
|
|
@ -799,6 +799,18 @@ This would display as "It is the 4th of September".
|
||||||
|
|
||||||
It is {% now "SHORT_DATETIME_FORMAT" %}
|
It is {% now "SHORT_DATETIME_FORMAT" %}
|
||||||
|
|
||||||
|
|
||||||
|
You can also use the syntax ``{% now "Y" as current_year %}`` to store the
|
||||||
|
output inside a variable. This is useful if you want to use ``{% now %}``
|
||||||
|
inside a template tag like :ttag:`blocktrans` for example::
|
||||||
|
|
||||||
|
{% now "Y" as current_year %}
|
||||||
|
{% blocktrans %}Copyright {{ current_year }}{% endblocktrans %}
|
||||||
|
|
||||||
|
.. versionadded:: 1.8
|
||||||
|
|
||||||
|
The ability to use the "as" syntax was added.
|
||||||
|
|
||||||
.. templatetag:: regroup
|
.. templatetag:: regroup
|
||||||
|
|
||||||
regroup
|
regroup
|
||||||
|
|
|
@ -419,6 +419,9 @@ Templates
|
||||||
* Added a :class:`locmem.Loader <django.template.loaders.locmem.Loader>`
|
* Added a :class:`locmem.Loader <django.template.loaders.locmem.Loader>`
|
||||||
class that loads Django templates from a Python dictionary.
|
class that loads Django templates from a Python dictionary.
|
||||||
|
|
||||||
|
* The :ttag:`now` tag can now store its output in a context variable with the
|
||||||
|
usual syntax: ``{% now 'j n Y' as varname %}``.
|
||||||
|
|
||||||
Requests and Responses
|
Requests and Responses
|
||||||
^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
|
|
@ -1697,6 +1697,8 @@ class TemplateTests(TestCase):
|
||||||
datetime.now().day, datetime.now().month, datetime.now().year)),
|
datetime.now().day, datetime.now().month, datetime.now().year)),
|
||||||
'now06': ('''{% now "j 'n' Y"%}''', {}, '''%d '%d' %d''' % (
|
'now06': ('''{% now "j 'n' Y"%}''', {}, '''%d '%d' %d''' % (
|
||||||
datetime.now().day, datetime.now().month, datetime.now().year)),
|
datetime.now().day, datetime.now().month, datetime.now().year)),
|
||||||
|
'now07': ('''{% now "j n Y" as N %}-{{N}}-''', {}, '''-%d %d %d-''' % (
|
||||||
|
datetime.now().day, datetime.now().month, datetime.now().year)),
|
||||||
|
|
||||||
### URL TAG ########################################################
|
### URL TAG ########################################################
|
||||||
# Successes
|
# Successes
|
||||||
|
|
Loading…
Reference in New Issue