Python django shell (ipython) unexpected behavior or error?

The Django environment behaves (at least for me) unexpectedly when working with locale settings. Validating the form of a decimal field, separated by a comma, works when called from an external script and does not work when called from the django shell (ipython).

Starting a new project I received the following files:

local_forms/ ├── local_forms │  ├── __init__.py │  ├── models.py │  ├── settings.py │  ├── urls.py │  └── wsgi.py ├── manage.py ├── my_form.py ├── test_form.py 

local_forms / models.py:

 from django.db import models class MyModel(models.Model): val=models.DecimalField("value",max_digits=11,decimal_places=2) 

my_form.py

 from django import forms from django.conf import settings from local_forms.models import MyModel class MyForm(forms.ModelForm): val = forms.DecimalField(localize=True) def __init__(self,*args,**kwargs): super(MyForm,self).__init__(*args,**kwargs) self.fields['val'].localize=True if __debug__: print self.fields['val'].localize print ("Separator: "+settings.DECIMAL_SEPARATOR) print ("Language: " +settings.LANGUAGE_CODE) class Meta: model=MyModel 

test_form.py:

 #!/usr/bin/env python import os import sys if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "local_forms.settings") import my_form form=my_form.MyForm({'val':'0,2'}) print ("Is bound: %s" % form.is_bound) print ("Form valid %s" % form.is_valid()) print("Errors in val: %s" % form['val'].errors) 

Calling / test_form.py gives:

 ./test_form.py True Separator: . Language: de-de Is bound: True Form valid True Errors in val: 

Performing the same task in django shell: python manage.py shell

 In [1]: import my_form as mf In [2]: form=mf.MyForm({'val':'0,2'}) True Separator: . Language: de-de In [3]: form.is_valid() Out[3]: False In [4]: form['val'].errors Out[4]: [u'Enter a number.'] 

To summarize: if I run the django shell (which uses ipython on my computer), the locale somehow does not work. Similarly, the script works fine. Could you explain this behavior?

+6
source share
1 answer

Django control commands, including shell , reset the language 'en-us' , therefore your problem. An explanation of this behavior is in the Django documentation :

By default, the BaseCommand.execute () method sets the hard-coded 'en-us locale, because some of the commands that come with Django perform several tasks (such as custom rendering of content and a collection of databases) that require a system-neutral language string ( for which we use "en-us").

Your example works in the shell if you activate the correct language:

 >>> from django.utils.translation import get_language >>> get_language() > 'en-us' >>> import my_form as mf >>> form=mf.MyForm({'val':'0,2'}) True Separator: . Language: de-de >>> form.is_valid() > False >>> from django.utils.translation import activate >>> activate('de-de') >>> get_language() > 'de-de' >>> form=mf.MyForm({'val':'0,2'}) True Separator: . Language: de-de >>> form.is_valid() > True 

On a side note, you should always check the current language using get_locale() instead of relying on settings .

+6
source

Source: https://habr.com/ru/post/943740/


All Articles