简体   繁体   中英

Django password = models.CharField(_('password'), max_length=128)

What relevance does the underscore have before ('password') in this line?

password = models.CharField(_('password'), max_length=128)

I looked at the Django documentation and it states that the first parameter in the model field definition is a 'verbose name', however I cannot find what relevance the underscore has in this case?

The whole file I am looking at is: https://github.com/django/django/blob/master/django/contrib/auth/models.py

Thanks, Mark

The underscore is a common way of denoting that this string is available for translation:

 from django.utils.translation import ugettext_lazy as _

This means the string will show up in tools that gather these strings and substitute them for translated strings (see GNU gettext for example). This approach is used not only in Python but also in other programming languages / projects that require translated strings.

If you were to change the language of your Django website it would know to display a different string instead of 'password' (depending on which translations are available, otherwise it'll default back to this English string).

Underscore might be for localization/translation. Check the imports. ex.

from django.utils.translation import ugettext_lazy as _

In this link https://github.com/django/django/blob/master/django/contrib/auth/models.py look for line containing below import

from django.utils.translation import ugettext_lazy as _

For international character set (Unicode) support, ugettext().You can use ugettext_lazy() as the default translation method for a particular file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM