Note that there are some explanatory texts on larger screens.

plurals
  1. POpython django shell (ipython) unexpected behavior or bug?
    primarykey
    data
    text
    <p>Django shell behaves (at least for me) unexpected when working with locale settings. Form validation of a comma separated decimal field works when calling from external script and fails on calling from django shell (ipython).</p> <p>Starting a new Project I got the following files:</p> <pre><code>local_forms/ ├── local_forms │   ├── __init__.py │   ├── models.py │   ├── settings.py │   ├── urls.py │   └── wsgi.py ├── manage.py ├── my_form.py ├── test_form.py </code></pre> <p>local_forms/models.py:</p> <pre><code>from django.db import models class MyModel(models.Model): val=models.DecimalField("value",max_digits=11,decimal_places=2) </code></pre> <p>my_form.py</p> <pre><code>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 </code></pre> <p>test_form.py:</p> <pre><code>#!/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) </code></pre> <p>Calling ./test_form.py yields:</p> <pre><code>./test_form.py True Separator: . Language: de-de Is bound: True Form valid True Errors in val: </code></pre> <p>Doing the same thing in django shell: python manage.py shell</p> <pre><code>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.'] </code></pre> <p>To sum up: If i start the django shell (which on my pc uses ipython) the locale somehow does not work. Doing exactly the same in a script works perfectly. Can you please explain this behavior?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload