PageView

Exploring Django Utils #1

Exploring Django Utils #1

Exploring the Django source code I ended up discovering some really nice utility functions that I wasn’t aware of. I thought about sharing with you guys in a form of reference-like article. There are great stuff there, so I decided to break this post into a few parts.


Crypto

Module: django.utils.crypto

get_random_string

Calling it without any parameters defaults the length to 12.

from django.utils.crypto import get_random_string

get_random_string()
'5QxAxqyhsyJM'

Your may pass the number of characteres you want:

get_random_string(50)
'lrWYnyxhnXpwmjHDzmdgTFaIi1j73cKD5fPDOPwuVBmmKxITYF'

And also the collection of characteres the random string will have:

get_random_string(12, '0123456789')
'805379737758'

Dates

Module: django.utils.dates

Basically it is a collection of commonly-used date structures.

WEEKDAYS
from django.utils.dates import WEEKDAYS
WEEKDAYS = {
    0: _('Monday'), 1: _('Tuesday'), 2: _('Wednesday'), 3: _('Thursday'), 4: _('Friday'),
    5: _('Saturday'), 6: _('Sunday')
}
WEEKDAYS_ABBR
from django.utils.dates import WEEKDAYS_ABBR
WEEKDAYS_ABBR = {
    0: _('Mon'), 1: _('Tue'), 2: _('Wed'), 3: _('Thu'), 4: _('Fri'),
    5: _('Sat'), 6: _('Sun')
}
MONTHS
from django.utils.dates import MONTHS
MONTHS = {
    1: _('January'), 2: _('February'), 3: _('March'), 4: _('April'), 5: _('May'), 6: _('June'),
    7: _('July'), 8: _('August'), 9: _('September'), 10: _('October'), 11: _('November'),
    12: _('December')
}

DateFormat

Module: django.utils.dateformat

The implementation of PHP date() style date formatting, which is used in the Django template filter date formatter. This is a great utility module.

Refer to the Date Template Filter reference guide for a list of format codes semantically ordered.

format

For the following examples, consider the now = timezone.now().

from django.utils.dateformat import format
from django.utils import timezone

now = timezone.now()  # datetime.datetime(2016, 8, 10, 20, 32, 36, 461069, tzinfo=<UTC>)
format(now, 'd M Y')
'10 Aug 2016'

Date and time:

format(now, 'd/m/Y H:i')
'10/08/2016 20:32'

DateParse

Module: django.utils.dateparse

Convert a formatted date string into date/time/datetime. If the string is well formatted but represents an invalid, the function will return None.

parse_date
from django.utils.dateparse import parse_date

parse_date('2016-08-10')
datetime.date(2016, 8, 10)
parse_time
from django.utils.dateparse import parse_time

parse_time('20:43:02')
datetime.time(20, 43, 2)
parse_datetime
from django.utils.dateparse import parse_datetime

parse_datetime('2016-08-10 20:32:36')
datetime.datetime(2016, 8, 10, 20, 32, 36)

HTML

Module: django.utils.html

urlize

Utility to turn urls into <a> tags.

from django.utils.html import urlize

urlize('You guys should visit this website www.google.com')
'You guys should visit this website <a href="http://www.google.com">www.google.com</a>'

It also works with emails:

urlize('Send me a message to vitor@freitas.com')
'Send me a message to <a href="mailto:vitor@freitas.com">vitor@freitas.com</a>'

You can also trim the size of the link:

urlize('Visit the new Snippets section https://simpleisbetterthancomplex.com/snippets/', 30)
'Visit the new Snippets section <a href="https://simpleisbetterthancomplex.com/snippets/">https://simpleisbetterthanc...</a>'

That’s it for now. I hope you may find some of those useful. I will cover more modules in a future article.

Keep reading: