Dateformat and escaped chars

73 views
Skip to first unread message

Waylan Limberg

unread,
Mar 13, 2006, 11:17:53 PM3/13/06
to django...@googlegroups.com
I'm trying to format my dates with escaped characters in the format.
However, I can only seem to get them to work with the 'now' template
tag, but not the 'date' filter. Some examples:

{% now "jS o\f F" %}

will return '13th of March' as it should. But if I do this in the template:

{{ post.pub_date|date:"jS o\f F" }}

I get '13th o9:54 March' instead.

from the shell:
>>> from django.utils.dateformat import *
>>> from datetime import datetime
>>> d = datetime.now()
>>> df = DateFormat(d)
>>> print df.format('jS o\f F')
13th o March
>>> df.format('jS o\f F')
'13th o\x0c March'

I just updated to lasted truck (revision 2521) with no change in
behavior. I'm running python 2.3 on Linux if that makes any
difference. I searched the tickets and didn't find any reference to my
problem. Am I missing something?
--
----
Waylan Limberg
way...@gmail.com

Malcolm Tredinnick

unread,
Mar 13, 2006, 11:39:43 PM3/13/06
to django...@googlegroups.com
On Mon, 2006-03-13 at 23:17 -0500, Waylan Limberg wrote:
> I'm trying to format my dates with escaped characters in the format.
> However, I can only seem to get them to work with the 'now' template
> tag, but not the 'date' filter. Some examples:
>
> {% now "jS o\f F" %}
>
> will return '13th of March' as it should. But if I do this in the template:
>
> {{ post.pub_date|date:"jS o\f F" }}
>
> I get '13th o9:54 March' instead.

OK, that's a bug. The problem is that django,util,dateformat.format is
being passed the format string with the backslash already removed. Not
sure why that is happening yet, but it's a real bug.

I am not going to have time to look at it this afternoon, so you should
probably open a ticket for it so that we don't lose the problem report.

> from the shell:
> >>> from django.utils.dateformat import *
> >>> from datetime import datetime
> >>> d = datetime.now()
> >>> df = DateFormat(d)
> >>> print df.format('jS o\f F')
> 13th o March
> >>> df.format('jS o\f F')
> '13th o\x0c March'

This one is all your own doing. :-)

Remember that Python will do the first pass of backslash-removals first,
before it gets to your function call. And '\f' is the form-feed
character (ASCII code 12), just like \n is newline and \t is tab. If you
want to pass in the two character '\' and 'f', you either need to use
raw strings -- r'jS o\f F' -- or escape the backslash -- 'jS o\\f S' --
when trying to construct the string in Python.

Cheers,
Malcolm


Waylan Limberg

unread,
Mar 14, 2006, 12:36:16 AM3/14/06
to django...@googlegroups.com
On 3/13/06, Malcolm Tredinnick <mal...@pointy-stick.com> wrote:
>
> On Mon, 2006-03-13 at 23:17 -0500, Waylan Limberg wrote:
> > I'm trying to format my dates with escaped characters in the format.
> > However, I can only seem to get them to work with the 'now' template
> > tag, but not the 'date' filter. Some examples:
> >
> > {% now "jS o\f F" %}
> >
> > will return '13th of March' as it should. But if I do this in the template:
> >
> > {{ post.pub_date|date:"jS o\f F" }}
> >
> > I get '13th o9:54 March' instead.
>
> OK, that's a bug. The problem is that django,util,dateformat.format is
> being passed the format string with the backslash already removed. Not
> sure why that is happening yet, but it's a real bug.
>
> I am not going to have time to look at it this afternoon, so you should
> probably open a ticket for it so that we don't lose the problem report.

Wow! I found a "real bug". Anyway, submitted as Ticket #1498.


>
> > from the shell:
> > >>> from django.utils.dateformat import *
> > >>> from datetime import datetime
> > >>> d = datetime.now()
> > >>> df = DateFormat(d)
> > >>> print df.format('jS o\f F')
> > 13th o March
> > >>> df.format('jS o\f F')
> > '13th o\x0c March'
>
> This one is all your own doing. :-)
>
> Remember that Python will do the first pass of backslash-removals first,
> before it gets to your function call. And '\f' is the form-feed
> character (ASCII code 12), just like \n is newline and \t is tab. If you
> want to pass in the two character '\' and 'f', you either need to use
> raw strings -- r'jS o\f F' -- or escape the backslash -- 'jS o\\f S' --
> when trying to construct the string in Python.

Doh! My bad.

Duncan Foster

unread,
Mar 14, 2006, 5:26:09 AM3/14/06
to django...@googlegroups.com
Waylan Limberg wrote:

>On 3/13/06, Malcolm Tredinnick <mal...@pointy-stick.com> wrote:
>
>
>>On Mon, 2006-03-13 at 23:17 -0500, Waylan Limberg wrote:
>>
>>
>>>I'm trying to format my dates with escaped characters in the format.
>>>However, I can only seem to get them to work with the 'now' template
>>>tag, but not the 'date' filter. Some examples:
>>>
>>> {% now "jS o\f F" %}
>>>
>>>will return '13th of March' as it should. But if I do this in the template:
>>>
>>> {{ post.pub_date|date:"jS o\f F" }}
>>>
>>>I get '13th o9:54 March' instead.
>>>
>>>
>>OK, that's a bug. The problem is that django,util,dateformat.format is
>>being passed the format string with the backslash already removed. Not
>>sure why that is happening yet, but it's a real bug.
>>
>>I am not going to have time to look at it this afternoon, so you should
>>probably open a ticket for it so that we don't lose the problem report.
>>
>>
>
>Wow! I found a "real bug". Anyway, submitted as Ticket #1498.
>
>

This is a duplicate of #1198. I took a look at this during the Django
sprint at PyCon. In django/template/__init__.py at line 524, the
contanst_arg is stripped of any backslashes. I'm not sure why this is
done; I'll leave the fix to those who understand the code a little
better than I do!

Duncan

Reply all
Reply to author
Forward
0 new messages