Ticket #15263: now_tag.diff

File now_tag.diff, 2.9 KB (added by Daniel Roseman, 14 years ago)
  • django/template/defaulttags.py

     
    381381
    382382    def render(self, context):
    383383        from datetime import datetime
    384         from django.utils.dateformat import DateFormat
    385         df = DateFormat(datetime.now())
    386         return df.format(self.format_string)
     384        from django.utils import formats
     385        from django.utils import dateformat
     386        now = datetime.now()
     387        try:
     388            return formats.date_format(now, self.format_string)
     389        except AttributeError:
     390            try:
     391                return dateformat.format(now, self.format_string)
     392            except AttributeError:
     393                return ''
    387394
    388395class SpacelessNode(Node):
    389396    def __init__(self, nodelist):
  • docs/ref/templates/builtins.txt

     
    680680
    681681Display the current date and/or time, according to the given string.
    682682
    683 Given format can be one of the predefined ones ``DATE_FORMAT``,
    684 ``DATETIME_FORMAT``, ``SHORT_DATE_FORMAT`` or ``SHORT_DATETIME_FORMAT``,
    685 or a custom format, same as the :tfilter:`date` filter. Note that predefined
     683The format string can be one of the predefined ones - ``DATE_FORMAT``,
     684``DATETIME_FORMAT``, ``SHORT_DATE_FORMAT`` or ``SHORT_DATETIME_FORMAT`` -
     685or a custom format, as with the :tfilter:`date` filter. Note that predefined
    686686formats may vary depending on the current locale.
    687687
    688688Example::
  • tests/regressiontests/templates/tests.py

     
    1818from django.template import loader
    1919from django.template.loaders import app_directories, filesystem, cached
    2020from django.utils import unittest
     21from django.utils.formats import date_format
    2122from django.utils.translation import activate, deactivate, ugettext as _
    2223from django.utils.safestring import mark_safe
    2324from django.utils.tzinfo import LocalTimezone
     
    13791380            'now02': ('{% now "j "n" Y"%}', {}, template.TemplateSyntaxError),
    13801381        #    'now03': ('{% now "j \"n\" Y"%}', {}, str(datetime.now().day) + '"' + str(datetime.now().month) + '"' + str(datetime.now().year)),
    13811382        #    'now04': ('{% now "j \nn\n Y"%}', {}, str(datetime.now().day) + '\n' + str(datetime.now().month) + '\n' + str(datetime.now().year))
     1383            # Check parsing of locale strings
     1384            'now05': ('{% now "DATE_FORMAT" %}', {},  date_format(datetime.now())),
    13821385
    13831386            ### URL TAG ########################################################
    13841387            # Successes
Back to Top