I accidentally wrote "{% now 123 %}" it outputs “2”.
Here i didn’t understand meaning of ‘2’ means here. it is a bug or feature ? there is no more information about the syntax of ‘now’ tag in documentation.
Well, there is the reference:
Such string can contain format specifiers characters as described in the
datefilter section.
The date filter gives the valid format specifiers.
The documentation doesn’t show what happens if you provide an invalid specifier. For that you would need to dig through the django source code. (That’s always the best approach for investigating undocumented behavior.)
The full sequence of functions / modules appears to be:
django.template.defaulttags.nowdjango.template.defaulttags.NowNodedjango.template.defaultfilters.datedjango.utils.formats.date_formatdjango.utils.dateformat.formatdjango.utils.dateformat.DateFormat
But, the root of the results you’re showing here is in the now method itself, where it’s creating the format_string using the slice [1:-1]. In other words, it’s using the second through the next-to-last character of the parameter - which in the case of the parameter being 123 results in 2. If you change this to {% now 12345 %}, you will see it returns 234.
If you pass it as a string, such as {% now "1234" %}, you will see it returns 1234.
Hi @KenWhitesell , Thanks for response. I really appreciate for detail information.
I would expect django to give me some information when provide int argument to now tag. for example it could be anything like {% now 234234 %} , {% now 8804.9 % }, whats your thought on this ?
First, if this is a serious concern or issue for you, I suggest you start making yourself familiar with the source code of the Django template language engine. You might also want to read How to create custom template tags and filters | Django documentation | Django to see how these arguments can be used.
Briefly, when it comes to parsing template tokens, there are no integers. Everything is generally parsed as strings. In the case of the now tag, it expects to get a quoted string as the argument. It assumes that the first and last character of the argument is a quote charater, and everything in between those quotes is the format string. The only purpose of having the quotes is to allow the argument string to contain spaces.
For example, you could write {% now $Y-m-d$ %} and it would yield 2025-12-28. Using $ as the first and last charater of the string instead of " is irrelevant because there are no embedded spaces. (This also means that {% now $Y m d$ %} will not work because the now tag will see that as three arguments instead of one.)
Other examples:
{% now Y %} => Dec. 28, 2025 (Effectively, no argument supplied)
{% now YMD %} => Dec (Only the M is used, the Y and D are stripped)
So no. Given how now is written and documented, I think it’s fine as it is.
django didn’t handle this {% lorem 99999999999999999999999999999999999999 w %} yet
Yes, and?
This is an absurdly large number. 10^38 ~= 2^129
(I love playing around with numbers like this.)
You would need a version of Python capable of handling a 130-bit address space, a few million spare universes with the space available to store the memory necessary to hold the result, along with an extra few billions of universes to mine for the raw materials to build it, and an extra universe to tap for enough power to run it.
You can cut the number of digits in half and still have a result that couldn’t be physically calculated or representated in existing computers.
I think django should document about lorem tag
what is noticed if i write {% lorem 2 %} then two lorem paragraph it will show but if i write {% lorem -2 %} it will return empty string.
It is documented. See lorem.
No information about dealing with negative values for count
There are lots of APIs that don’t document the effect of certain invalid inputs. In all cases, an invalid input can be considered to produce indeterminate effects. In this case, no output would be a logical conclusion from the description. (What would you expect to get for requesting -2 words?)