Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
11cac1bd
Kaydet (Commit)
11cac1bd
authored
Şub 05, 2015
tarafından
Curtis
Kaydeden (comit)
Tim Graham
Tem 01, 2015
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #4960 -- Added "strip" option to CharField
üst
b535eb3f
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
48 additions
and
11 deletions
+48
-11
fields.py
django/forms/fields.py
+9
-7
fields.txt
docs/ref/forms/fields.txt
+17
-3
1.9.txt
docs/releases/1.9.txt
+10
-1
test_fields.py
tests/forms_tests/tests/test_fields.py
+12
-0
No files found.
django/forms/fields.py
Dosyayı görüntüle @
11cac1bd
...
...
@@ -210,8 +210,10 @@ class Field(six.with_metaclass(RenameFieldMethods, object)):
class
CharField
(
Field
):
def
__init__
(
self
,
max_length
=
None
,
min_length
=
None
,
*
args
,
**
kwargs
):
self
.
max_length
,
self
.
min_length
=
max_length
,
min_length
def
__init__
(
self
,
max_length
=
None
,
min_length
=
None
,
strip
=
True
,
*
args
,
**
kwargs
):
self
.
max_length
=
max_length
self
.
min_length
=
min_length
self
.
strip
=
strip
super
(
CharField
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
if
min_length
is
not
None
:
self
.
validators
.
append
(
validators
.
MinLengthValidator
(
int
(
min_length
)))
...
...
@@ -222,7 +224,10 @@ class CharField(Field):
"Returns a Unicode object."
if
value
in
self
.
empty_values
:
return
''
return
smart_text
(
value
)
value
=
force_text
(
value
)
if
self
.
strip
:
value
=
value
.
strip
()
return
value
def
widget_attrs
(
self
,
widget
):
attrs
=
super
(
CharField
,
self
)
.
widget_attrs
(
widget
)
...
...
@@ -539,6 +544,7 @@ class RegexField(CharField):
error_message is an optional error message to use, if
'Enter a valid value' is too generic for you.
"""
kwargs
.
setdefault
(
'strip'
,
False
)
# error_message is just kept for backwards compatibility:
if
error_message
is
not
None
:
warnings
.
warn
(
...
...
@@ -1231,10 +1237,6 @@ class GenericIPAddressField(CharField):
class
SlugField
(
CharField
):
default_validators
=
[
validators
.
validate_slug
]
def
clean
(
self
,
value
):
value
=
self
.
to_python
(
value
)
.
strip
()
return
super
(
SlugField
,
self
)
.
clean
(
value
)
class
UUIDField
(
CharField
):
default_error_messages
=
{
...
...
docs/ref/forms/fields.txt
Dosyayı görüntüle @
11cac1bd
...
...
@@ -361,7 +361,7 @@ For each field, we describe the default widget used if you don't specify
Otherwise, all inputs are valid.
* Error message keys: ``required``, ``max_length``, ``min_length``
Has t
wo
optional arguments for validation:
Has t
hree
optional arguments for validation:
.. attribute:: max_length
.. attribute:: min_length
...
...
@@ -369,6 +369,13 @@ For each field, we describe the default widget used if you don't specify
If provided, these arguments ensure that the string is at most or at least
the given length.
.. attribute:: strip
.. versionadded:: 1.9
If ``True`` (default), the value will be stripped of leading and
trailing whitespace.
``ChoiceField``
~~~~~~~~~~~~~~~
...
...
@@ -824,8 +831,15 @@ For each field, we describe the default widget used if you don't specify
A regular expression specified either as a string or a compiled regular
expression object.
Also takes ``max_length`` and ``min_length``, which work just as they do for
``CharField``.
Also takes ``max_length``, ``min_length``, and ``strip``, which work just
as they do for ``CharField``.
.. attribute:: strip
.. versionadded:: 1.9
Defaults to ``False``. If enabled, stripping will be applied before the
regex validation.
.. deprecated:: 1.8
...
...
docs/releases/1.9.txt
Dosyayı görüntüle @
11cac1bd
...
...
@@ -302,6 +302,11 @@ Forms
* You can now :ref:`specify keyword arguments <custom-formset-form-kwargs>`
that you want to pass to the constructor of forms in a formset.
* :class:`~django.forms.CharField` now accepts a
:attr:`~django.forms.CharField.strip` argument to strip input data of leading
and trailing whitespace. As this defaults to ``True`` this is different
behavior from previous releases.
Generic Views
^^^^^^^^^^^^^
...
...
@@ -552,7 +557,7 @@ Database backend API
SQLite backend to add time lookups (hour, minute, second) to
:class:`~django.db.models.TimeField`, and may be needed by third-party
database backends.
* The ``DatabaseOperations.datetime_cast_sql()`` method (not to be confused
with ``DatabaseOperations.datetime_cast_date_sql()`` mentioned above)
has been removed. This method served to format dates on Oracle long
...
...
@@ -822,6 +827,10 @@ Miscellaneous
be serialized contains any control characters not allowed in the XML 1.0
standard, the serialization will fail with a :exc:`ValueError`.
* :class:`~django.forms.CharField` now strips input of leading and trailing
whitespace by default. This can be disabled by setting the new
:attr:`~django.forms.CharField.strip` argument to ``False``.
.. _deprecated-features-1.9:
Features deprecated in 1.9
...
...
tests/forms_tests/tests/test_fields.py
Dosyayı görüntüle @
11cac1bd
...
...
@@ -164,6 +164,18 @@ class FieldsTests(SimpleTestCase):
self
.
assertEqual
(
f
.
widget_attrs
(
PasswordInput
()),
{
'maxlength'
:
'10'
})
self
.
assertEqual
(
f
.
widget_attrs
(
Textarea
()),
{
'maxlength'
:
'10'
})
def
test_charfield_strip
(
self
):
"""
Ensure that values have whitespace stripped and that strip=False works.
"""
f
=
CharField
()
self
.
assertEqual
(
f
.
clean
(
' 1'
),
'1'
)
self
.
assertEqual
(
f
.
clean
(
'1 '
),
'1'
)
f
=
CharField
(
strip
=
False
)
self
.
assertEqual
(
f
.
clean
(
' 1'
),
' 1'
)
self
.
assertEqual
(
f
.
clean
(
'1 '
),
'1 '
)
# IntegerField ################################################################
def
test_integerfield_1
(
self
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment