ignore field during form validation

2012-07-26 Thread Zoltan Szalai

Hi all,

Let's assume i have to the following simple form:


class Form(forms.Form):

check = forms.BooleanField(
required=False,
)
# take into account only when 'check' is True
len = forms.IntegerField(
min_value=3,
max_value=5,
required=True,
)


What I want is to validate the 'len' field only when 'check' is True.
I could define the clean method of the form and validate the required,
min_value and max_value stuff only when 'check' is True but the case when
someone types a non integer value into the input is still there. How 
could I skip that? That check is done by the IntegerField.


The best would be to simply remove the 'len' field of the form when 
'check' is False.
Maybe I could examine request.POST in form's __init__ and according to 
it's content remove the fields I don't care about.

Or is that a terrible idea?


bests
Zoli

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: ignore field during form validation

2012-07-26 Thread Zoltan Szalai

On 2012.07.26. 17:44, Tomas Neme wrote:

class Form(forms.Form):

 check = forms.BooleanField(
 required=False,
 )
 # take into account only when 'check' is True
 len = forms.IntegerField(
 min_value=3,
 max_value=5,
 required=True,
 )


What I want is to validate the 'len' field only when 'check' is True.
I could define the clean method of the form and validate the required,
min_value and max_value stuff only when 'check' is True but the case when
someone types a non integer value into the input is still there. How could I
skip that? That check is done by the IntegerField.

well, you could override clean_len() and not do anything in it, and
then override clean() and do your check there.



I don't think that would help. The clean method of the Field 
(IntegerField in this case) would still run.




--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: ignore field during form validation

2012-07-26 Thread Zoltan Szalai
In the end I came up with a solution which seems a bit hacky but does 
exactly what I want.

It looks like this:

def clean(self):
cleaned_data = super(Form, self).clean()
check = cleaned_data.get("check", None)

# ignore 'len' when 'check' is False
if not checkand "len" in self._errors:
del self._errors["len"]

return cleaned_data


one thing to note: "len" won't be available in cleaned_data but that's 
just cool since it's not valid :)



On 2012.07.26. 18:29, Kurtis Mullins wrote:
You could remove "required=True" from 'len' and override clean() to 
perform a multiple-field validation. (pretty much what Thomas mentioned)


len() will execute -- it will check to see if it's an integer. If you 
just want to completely ignore it, then do exactly as Thomas said, 
override. But you'll have to make sure you do the "Integer Validation" 
check in your clean() method if you ignore that validation in clean_len().


clean__len():
return self.cleaned_data['len']

On Thu, Jul 26, 2012 at 12:21 PM, Zoltan Szalai <mailto:defaultd...@gmail.com>> wrote:


On 2012.07.26. 17 :44, Tomas Neme wrote:

class Form(forms.Form):

 check = forms.BooleanField(
 required=False,
 )
 # take into account only when 'check' is True
 len = forms.IntegerField(
 min_value=3,
 max_value=5,
 required=True,
 )


What I want is to validate the 'len' field only when
'check' is True.
I could define the clean method of the form and validate
the required,
min_value and max_value stuff only when 'check' is True
but the case when
someone types a non integer value into the input is still
there. How could I
skip that? That check is done by the IntegerField.

well, you could override clean_len() and not do anything in
it, and
then override clean() and do your check there.


I don't think that would help. The clean method of the Field
(IntegerField in this case) would still run.



--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



create sql functions before tests

2012-10-25 Thread Zoltan Szalai

Hi,

Do you have any idea how could I run some custom sql that creates some C 
functions before tests start to run?

django: 1.3.4, postgres 9.1

thx
Zoli

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: create sql functions before tests

2012-10-27 Thread Zoltan Szalai
Never mind, I just ended up modifying the default postgres template. 
Altough a setting would be great where you could define the template the 
test db is created from.


On 2012.10.26. 2:04, Zoltan Szalai wrote:

Hi,

Do you have any idea how could I run some custom sql that creates some 
C functions before tests start to run?

django: 1.3.4, postgres 9.1

thx
Zoli



--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Textarea and initial value

2013-09-30 Thread Zoltan Szalai
try not to use the same value ('about') for the name attribute of the 
textarea and your submit button.

not sure it helps the situation though.


On 2013.09.30. 19:13, Marcin Szamotulski wrote:

ps. If I render the form with
enctype='multipart/form-data'>

{{ form.as_ul() }}

I get the same behaviour.

Marcin


On 30 September 2013 17:11, Marcin Szamotulski > wrote:


Sure here it is how the form is render:

 


Submit


Format:

Text
reStructuredText
MarkDown



Profile image:






I use jinja2 template language and it is rendered with

{% csrf_token %}
{{ render_field(aboutform.about, label=False) }}
{{ render_field(aboutform.format) }}
{{ render_field(aboutform.image) }}



where render_field is a simple macro:

{% macro render_field(field, inline=True, after=True, label=True) -%}
{% if inline and field.field.widget.__class__.__name__ ==
'CheckboxInput' -%}
{{ render_boolfield(field, after) }}
{%- else -%}
{% if label -%}
{{ field.label_tag() }}
{%- endif %}
{{ field.as_widget() }}

{{ field.errors.as_ul() }}

{%- endif %}
{%- endmacro %}

Thanks,
Marcin



On 30 September 2013 15:11, Daniel Roseman mailto:dan...@roseman.org.uk>> wrote:

On Monday, 30 September 2013 15:41:47 UTC+1, Marcin
Szamotulski wrote:


I don't do anything odd: I simply modify it by typing in
side textarea.
The page has a very simple JavaScript which does not do
anything with
the textarea (though I double-checked that turning it off
does not
improve the situation).  Inspecting the request reveals
that the data is
indeed submitted twice (thus django correctly interpret
the situation).

The form is using enctype='multipart/form-data' since it
is also used to
submit a file, though leaving only the textarea inside the
form and
removing the enctype does not help.

I also checked this in both firefox and chrome and both
show that the
POST data for this textarea is submitted twice.

Any ideas?

Thanks for help,
Marcin


Can you perhaps post the template fragment that renders the
field, and also the rendered HTML?
--
DR.



--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/5249B248.2000705%40gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.