Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

modelform field name can't be localized when in production

I have a model:

class Server(models.Model):
     serverId = models.IntegerField(verbose_name=_("serverId"))
     name = models.CharField(max_length=200, verbose_name=_("server_name"))
     ip = models.CharField(max_length=200, verbose_name=_("ip"))
     cport = models.IntegerField(default=5000, verbose_name=_("cport"))
     aport = models.IntegerField(default=1000, verbose_name=_("aport"))
     hport = models.IntegerField(default=2000, verbose_name=_("hport"))
     version = models.CharField(max_length=100, verbose_name=_("version"))
     serverGroup = models.ForeignKey(Group, null=True, blank=True,
             verbose_name=_('server_group'))
     class Meta:
         db_table = u'server'

     def __unicode__(self):
         return self.name

and the modelform:

class ServerForm(ModelForm):
    class Meta:
        model = Server

from within this app directory I did

$ mkdir locale
$ django-admin.py makemessages -l zh_CN

then I provided the translation in locale/zh_CN/LC_MESSAGES/django.po then I did

$ django-admin.py compilemessages

then I ran the development server:

$ python manage.py runserver

and went to look at the url http://127.0.0.1:8000 in firefox and the translation displayed. So I thought I did right and I deployed the project on the same machine using nginx + fastcgi with nothing changed in the whole project. Then I go to the url http://127.0.0.1, and then the the modelform shows English there. It didn't localize to Chinese.

I've googled much and read many docs from docs.djangoproject.com and still don't know how to solve the problem. So I ask here.

I only set LANGUAGE_CODE = 'zh_CN' in my settings.py and leave everything on deafult. My django version is 1.2.4

Any of your comments are appreciated.

like image 500
iDesperadO Avatar asked Feb 25 '11 02:02

iDesperadO


People also ask

How do you exclude a specific field from a ModelForm?

Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.

How do you remove this field is required Django?

If yes try to disable this behavior, set the novalidate attribute on the form tag As <form action="{% url 'new_page' %}", method="POST" novalidate> in your html file.

How can we make field required in Django?

Let's try to use required via Django Web application we created, visit http://localhost:8000/ and try to input the value based on option or validation applied on the Field. Hit submit. Hence Field is accepting the form even without any data in the geeks_field. This makes required=False implemented successfully.


1 Answers

Make sure you are using lazy_translation. Are you importing ugettext_lazy or ugettext?

from django.utils.translation import ugettext_lazy as _

http://docs.djangoproject.com/en/dev/topics/i18n/internationalization/#lazy-translation

like image 107
Yuji 'Tomita' Tomita Avatar answered Sep 16 '22 18:09

Yuji 'Tomita' Tomita