A simple Python function that converts a Python datetime formatting string to its nearest PHP equivalent.
Python and PHP use different format string conventions when specifying datetime formats:
* Python: [https://docs.python.org/2/library/time.html#time.strftime](https://docs.python.org/2/library/time.html#time.strftime)
* PHP: [http://php.net/manual/en/function.date.php](http://php.net/manual/en/function.date.php)
Working with Django the Date, Time and DateTime widgets all use Python format strings as stored in:
* django.conf.global_settings.DATE_INPUT_FORMATS
* django.conf.global_settings.TIME_INPUT_FORMATS
* django.conf.global_settings.DATETIME_INPUT_FORMATS
but if you want to use a datetime widget like the datetimepicker here:
[http://xdsoft.net/jqplugins/datetimepicker/](http://xdsoft.net/jqplugins/datetimepicker/)
then you'll find it uses PHP format specifiers.
If you want Django and the datetimepicker to populate a field in the same way, you need to ensure they use the same format.
So I add to the Django from context the default format as follows:
`context["default_datetime_input_format"] = datetime_format_python_to_PHP(DATETIME_INPUT_FORMATS[0])`
and in the template Javascript on my form for the datetimepicker i give it:
`"format": {{default_datetime_input_format}}`
and the datetimepicker now populates the the datetime field in the same format as Django.
Converts an integer or floating-point number or a string to a string containing the delimiter character (default comma) after every delimeter_count digits (by default 3 digits)
This is an enhancement of snippet [#172](http://djangosnippets.org/snippets/172/). Here I use [BeautifulSoup](http://www.crummy.com/software/BeautifulSoup/) — far more easier to install through pip in a virtualenv, and possibly a bit more maintained — to format and properly indent the rendered HTML from templates.
I also added a check to only tidy contents in a `DEBUG=True` environment, regarding high impact on performance while doing so in production.
Last, it's compatible with Django 1.2.x.
I wanted a way to allow flexible phone number validation while making sure the saved data was uniform.
ex.
With:
RegexFormatField(r'^\(?(?P<area>\d{3})\)?[-\s.]?(?P<local>\d{3})[-\s.]?(?P<subscriber>\d{4})$',
format='%(area)s %(local)s-%(subscriber)s')
input:
(444) 444-4444
444 444-4444
444-444-4444
444.444.4444
4444444444
output:
444 444-4444
Ever wished you could have pretty SQL-like output for a python object (e.g., a list of dicts) while you're debugging your code? This function will do just that. Simply pass it an object that is an iterable of dictionaries and it returns it in an easy-to-read table, similar to the output of commandline SQL.
Example:
from tablelize import tableize
from django.contrib.auth.models import User
print(tableize(User.objects.values('email', 'first_name', 'last_name')))
+------------+-----------+-------------------+
| first_name | last_name | email |
+------------+-----------+-------------------+
| Test | User | [email protected] |
| Another | User | [email protected] |
+------------+-----------+-------------------+
I have a need to conditionally format a negative number, a hedgefund's daily price change, Excel style. i.e. show a negative number as a parenthesized number instead of a negative sign. Here is a filter that will do that and more, solving a more general case. See the doctest for examples.
Template filter to format a start and end time in to a range. Uses Django's ["P" format](http://www.djangoproject.com/documentation/templates/#now) and assumes start and end time are on the same day or night before/morning after.
`{{ start_time|time_range:end_time }}`
Examples:
7-8 p.m.
8 p.m. - midnight
noon - 4 p.m.
9:45 a.m. - 5:15 p.m.
10:30 p.m. - 1:30 a.m.
I use this filter quite a bit to keep my templates less cluttered. Instead of:
{%if some_variable%}, {{some_variable}}{%endif%}
I can write:
{{some_variable|format:", %s"}}
A common one I use is:
{{some_variable|format:"<p>%s</p>"}}
I just converted the autop filter from Drupal (which is itself based on a Wordpress filter) from PHP to Python. I had to change the format of the regular expressions a bit and make them raw strings, but otherwise the function is unchanged. It should work exactly like the original function.
This is a reasonably straight forward port of functionality provided by the `django.utils.dateformat` module into a method extending JavaScript's Date object. Its intended use is to allow client-side dynamic content to share the same date & time string formatting as Django template markup. By using this in conjunction with a context processor (to pass a format string to all templates) you can switch formats for both server-generated & client-generated dates across a complete site with a single setting.
The function supports *almost* the entire format -- as listed by the Django documentation for the [now template tag](http://www.djangoproject.com/documentation/templates/#now) -- with the exception of "I" & "T".
As a 'dumb' illustration, the following template tag usage:
It is {% now "jS F Y H:i" %}
...could equate to the following:
It is <script type="text/javascript">var now = new Date(); document.write(now.strfdate('jS F Y H:i'));</script>
It's not extensively tested (I only wrote it over the weekend), but seems to be working okay. Feel free to leave any corrections or suggestions in the comments, and I'll try to keep this entry updated if I make any fixes or changes.