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.
Your may pass the number of characteres you want:
And also the collection of characteres the random string will have:
Dates
Module: django.utils.dates
Basically it is a collection of commonly-used date structures.
WEEKDAYS
WEEKDAYS_ABBR
MONTHS
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().
Date and time:
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
parse_time
parse_datetime
HTML
Module: django.utils.html
urlize
Utility to turn urls into <a> tags.
It also works with emails:
You can also trim the size of the link:
That’s it for now. I hope you may find some of those useful. I will cover more modules in a future article.