I want to use translation in Django forms (and ModelForms), but the only solution I've found is using the 'label' attribute in the form fields
('from django.utils.translation import ugettext as _' is omitted):
forms.py:
class MyForm(ModelForm):
name = forms.CharField(label=_('Name'))
description = forms.CharField(label=_("Description"))
class Meta:
model = models.MyModel
fields = ('name', 'description')
models.py:
class MyModel (models.Model):
name = models.CharField(max_length=50,verbose_name=_("Name"))
description = models.CharField(max_length=200, verbose_name=_("Description"))
In ticket #3600 (http://code.djangoproject.com/ticket/3600) tells us that label only can be translated with 'gettext' during initialization, and this happens to me too.
Can somebody help me? I think must exist a way to translate fields form for a model...
Thanks!
You need to use ugettext_lazy() instead of ugettext().
>
> forms.py:
> class MyForm(ModelForm):
> name = forms.CharField(label=_('Name'))
> description = forms.CharField(label=_("Description"))
> class Meta:
> model = models.MyModel
> fields = ('name', 'description')
>
> models.py:
> class MyModel (models.Model):
> name = models.CharField(max_length=50,verbose_name=_("Name"))
> description = models.CharField(max_length=200, verbose_name=_("Description"))
>
--
Ramiro Morales
http://rmorales.net
*sigh* Will people please stop *guessing* at what goes into the
django.root option? Don't use options you don't understand what they're
for and if you work out that a particular option is needed, understand
what it does before filling it in. :-(
The documentation is pretty clear on what it should contain:
http://docs.djangoproject.com/en/dev/howto/deployment/modpython/#basic-configuration
> PythonDebug On
> PythonPath "['/home/rex/django/mturk'] + sys.path"
> </Location>
>
> Some sources say this snippet should go in /etc/apache2/httpd.conf,
> and some sources say it should go in /etc/apache2/sites-enabled/000-
> default. Which should I go with?
It depends on which distribution you're running. On Fedora, for example,
it goes in /etc/httpd/conf.d/ . So you'll need to provide some more
information.
> Also, I read somewhere that I should
> change my DocumentRoot to the path to the Django project, but wouldn't
> that expose my database and my Python source?
The Django documentation for installing with modpython is accurate and
complete, so use that as your reference (then using the Apache manual
and other places to understand the terms in there if you're not sure).
The above advice is not just bad, it's dangerous and stupid. So you're
right to be suspicious. There's simply no reason to do that.
Regards,
Malcolm
[snip]
File "/var/lib/python-support/python2.5/django/core/handlers/
base.py", line 44, in load_middleware
raise exceptions.ImproperlyConfigured, 'Middleware module "%s"
does not define a "%s" class' % (mw_module, mw_classname)
ImproperlyConfigured: Middleware module
"django.contrib.flatpages.middleware" does not define a
"FlatpageFallbackMiddlewar" class