Working Ninja
2016-03-30T21:57:36
Print First Character from Django QuerySet

I was attempting to make a directory listing of sorts earlier tonight and couldn't think up a clean solution. Some surfing on the world wide web offered up a quick template tag solution.

For example, say we have a list of objects (Person) from our app's view, [<Person: Doe>, <Person: Li>, <Person: Smith>, ...], and want to only print the first letter of their last name.

We can do this by first taking the string person.last_name and turning it into a list. Then, to snag just the first item from our newly formed list.

Template Tag with Filter(s) Rendered Output
{{ person.last_name }} Doe
{{ person.last_name|make_list }} ['D', 'o', 'e']
{{ person.last_name|make_list|first }} D

Profit!

Source: http://stackoverflow.com/questions/26058602/want-to-display-the-first-character-of-word-in-django-templates