views:

252

answers:

3

Why does this Django code use _ in front of 'has favicon'

has_favicon = models.BooleanField(_('has favicon'))
+10  A: 

If you look in the import statements, you'll find that they tied _ to a function that turns stuff into unicode and localizes it by writing:

from django.utils.translation import ugettext_lazy as _
SapphireSun
added part about localization thanks to just somebody
SapphireSun
+8  A: 

_ is usually a macro/function from gettext, it means the argument is a localized string. this is not limited to Django or Python. in fact gettext is originally a package for C programs, ported to many other languages over the years.

just somebody
added part about gettext heritage. you can copy it as well. ;)
just somebody
It also marks it as needing translation so automated tools can create a *.po file for translators to use.
Matthew Talbert
+5  A: 

_ in django is a convention that is used for localizing texts. It is an alias for ugettext_lazy. Read here for more info about it.

jpartogi
+1 for linking to the docs.
Boldewyn