Sunday, January 29, 2017

Re: form_invalid

BTW, IIRC, you can also make your model fields required (for db
consistency) but then you need to use django.forms.Form instead of
django.forms.ModelForm.



2017-01-30 0:08 GMT-03:00 Norberto Bensa <nbensa@gmail.com>:
> Then you need to make your model fields not required and override your
> form.clean() methods, then you'll have complete control over the
> validation process.
>
> I see Melvyn already aswered.
>
>
>
> 2017-01-29 18:04 GMT-03:00 Roberto Russi <robrussi.reg@gmail.com>:
>> But fields are required! I need to have a control over form also if 'save'
>> button is clicked and required fields are not filled.
>>
>> Il giorno domenica 29 gennaio 2017 20:26:10 UTC+1, Norberto Bensa ha
>> scritto:
>>>
>>> Are you telling me the following doesn't work for you?
>>>
>>> class DemoCreateView(CreateView):
>>> ...
>>> def post(self, request, *args, **kwargs):
>>> print(request.POST)
>>> return super().post(request, *args, **kwargs)
>>> ...
>>>
>>>
>>> Now, at the console:
>>>
>>>
>>> <QueryDict: {'csrfmiddlewaretoken':
>>> ['A3hHKM6Bs6NjJA996ALAsYPcYR7M3NWkzP4wcd6WUX1CEazC1dAK3aD1n8zl1aUU'],
>>> 'demo_desc
>>> r': [''], 'demo_code': ['']}>
>>>
>>> ...
>>>
>>> BTW, I had to add "blank=True, null=True" to your models, otherwise
>>> the fields are marked as required and the browser does validate that.
>>>
>>> HTH,
>>> Norberto
>>>
>>>
>>> 2017-01-29 15:54 GMT-03:00 Roberto Russi <robrus...@gmail.com>:
>>> >
>>> > I have tried it but post method is called only if form is valid and not
>>> > if there are mandatory field not filled.
>>> >
>>> >
>>> > Il giorno domenica 29 gennaio 2017 18:30:50 UTC+1, Norberto Bensa ha
>>> > scritto:
>>> >>
>>> >> https://ccbv.co.uk is your friend with class based views.
>>> >>
>>> >> I'd override post(self, request, *args, **kwargs), in request.POST,
>>> >> you'll have your form's data (before it's validated).
>>> >>
>>> >> HTH,
>>> >> Norberto
>>> >>
>>> >> 2017-01-29 9:10 GMT-03:00 Roberto Russi <robrus...@gmail.com>:
>>> >>>
>>> >>> I need get data from a form when submit is clicked even if data are
>>> >>> invalid, inconplete or empty.
>>> >>> I try to override form_invalid in CreateView but it is never called
>>> >>> and form_valid, obviously, only if data are valid.
>>> >>> Why form_invalid is never called?
>>> >>>
>>> >>> I have this test project with django 1.10.5 and python 3.5
>>> >>>
>>> >>> models.py
>>> >>>
>>> >>> class Demo(models.Model):
>>> >>> demo_descr = models.CharField("Description",
>>> >>> max_length=50,
>>> >>> )
>>> >>> demo_code = models.CharField("Code",
>>> >>> max_length=5,
>>> >>> )
>>> >>>
>>> >>> def __str__(self):
>>> >>> return self.demo_descr
>>> >>>
>>> >>> class Meta:
>>> >>> verbose_name = "Test"
>>> >>> verbose_name_plural = "Tests"
>>> >>> ordering = ["demo_descr"]
>>> >>>
>>> >>> views.py
>>> >>>
>>> >>> class DemoForm(forms.ModelForm):
>>> >>> class Meta:
>>> >>> model = models.Demo
>>> >>> fields = ('demo_descr','demo_code')
>>> >>>
>>> >>>
>>> >>> class DemoCreateView(CreateView):
>>> >>> form_class = DemoForm
>>> >>> success_url = 'demo'
>>> >>> template_name = 'test_form.html'
>>> >>> model = models.Demo
>>> >>>
>>> >>> def form_valid(self, form):
>>> >>> print('valid')
>>> >>> return CreateView.form_valid(self, form)
>>> >>>
>>> >>> def form_invalid(self, form):
>>> >>> print('invalid')
>>> >>> return CreateView.form_invalid(self, form)
>>> >>>
>>> >>> urls.py
>>> >>>
>>> >>> url(r'^demo$', views.DemoCreateView.as_view(), name='demo'),
>>> >>>
>>> >>>
>>> >>> test_form.html
>>> >>>
>>> >>> <form method="post" action="">
>>> >>> {% csrf_token %}
>>> >>> {{ form.as_p }}
>>> >>> <input type="submit" value="Save" />
>>> >>> </form>
>>> >>>
>>> >>> --
>>> >>> 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...@googlegroups.com.
>>> >>> To post to this group, send email to django...@googlegroups.com.
>>> >>> Visit this group at https://groups.google.com/group/django-users.
>>> >>> To view this discussion on the web visit
>>> >>> https://groups.google.com/d/msgid/django-users/c4ab5b85-13cc-4569-aa0d-5ef4dd6911a4%40googlegroups.com.
>>> >>> For more options, visit https://groups.google.com/d/optout.
>>> >>
>>> >>
>>> > --
>>> > 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...@googlegroups.com.
>>> > To post to this group, send email to django...@googlegroups.com.
>>> > Visit this group at https://groups.google.com/group/django-users.
>>> > To view this discussion on the web visit
>>> > https://groups.google.com/d/msgid/django-users/1262e431-5210-4707-bea6-0e8d8813c33b%40googlegroups.com.
>>> >
>>> > For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> 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+unsubscribe@googlegroups.com.
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/59d268d9-269e-4875-a405-bc9c7985e506%40googlegroups.com.
>>
>> For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CADut3oBvcEMOYeNoV_-0Wjvn4jE32WDPP0g9%3D7%3DW00HV%2B1Xv3g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment