Friday, May 6, 2016

Re: Django forms validation

Good morning Lekan. My response is inline...
On May 6, 2016 4:21 AM, "Lekan Wahab" <olamyy53@gmail.com> wrote:
>
> Hello Babatunde,
> Thank you for answering my question.
> I have actually done that.
> But Django throws and error whenever I I instantiate form/for_class to ContactForm(request. POST).
> It says it's expected only a single argumen. but two was provided.
>
That means you defined your form class without any arguments. I've looked at your form definition and that's exactly what's happening.

>> >> class ContactForm(forms.ModelForm):
>> >>     helper = FormHelper()
>> >>     helper.form_tag = False
>> >>     def __init__(self):

# __init__ is given only one argument. You didn't supply all the other arguments needed for the form to work properly.

>> >>         super(ContactForm, self).__init__()
>> >>         for key in self.fields:
>> >>             self.fields[key].required= False
>> >>             self.fields[key].label = False
>> >>          
>> >>
>> >>     class Meta:
>> >>         fields = '__all__'
>> >>         model =  Contact
> On 05 May 2016 10:36 PM, "Babatunde Akinyanmi" <tundebabzy@gmail.com> wrote:
>>
>> Hello Lekan. I have responded inline but my answer might not be a complete fix.....
>>
>> On May 5, 2016 4:56 PM, "Lekan Wahab" <olamyy53@gmail.com> wrote:
>> >
>> > Hello,
>> > Please, i have a model form in forms.py which am trying to validate and update my database with. However, for some reason the form is not being validated. I have tried everything i could think of. It just reloads the page.
>> >
>> > views.py
>> >>
>> >> from django.shortcuts import render
>> >> from information.forms import ContactForm
>> >> from django.http import HttpResponse, HttpResponseRedirect
>> >>
>> >>
>> >> def index(request):
>> >>     form_class = ContactForm
>> >>     if request.method == 'POST':
>> >>         form = form_class()
>>
>> The above line creates an empty form because you didn't supply anything as the data argument therefore the is_valid method will never return True. You need to instantiate it with your POST data. Try:
>>              form = form_class(request.POST)
>> >>         if form.is_valid():
>> >>             ###Dummy text to check validation
>> >>             return  HttpResponse('Done')
>> >>     return render(request, 'information/index.html', {'form': form_class})
>> >>
>>
>> {'form': form_class}
>> form_class is not an object. Perhaps you meant {'form': form}
>> >>
>> >> def complete(request):
>> >>     return HttpResponse("Thank you for contacting us")
>> >
>> >
>> > models.py
>> >  
>> >>
>> >> from crispy_forms.helper import FormHelper
>> >> from django import forms
>> >> from django.core.exceptions import ValidationError
>> >> from django.forms import Form
>> >> from information.models import Contact
>> >> from crispy_forms.helper import FormHelper
>> >> from crispy_forms.layout import Layout,Fieldset, HTML
>> >> from crispy_forms.bootstrap import TabHolder,Tab
>> >>
>> >>
>> >> class ContactForm(forms.ModelForm):
>> >>     helper = FormHelper()
>> >>     helper.form_tag = False
>> >>     def __init__(self):
>> >>         super(ContactForm, self).__init__()
>> >>         for key in self.fields:
>> >>             self.fields[key].required= False
>> >>             self.fields[key].label = False
>> >>          
>> >>
>> >>     class Meta:
>> >>         fields = '__all__'
>> >>         model =  Contact
>> >
>> >
>> > Thank you.
>> >
>> > --
>> > 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/CAE6v7oeD7Qb_%2BJrhiOzRTiDJ%3DqEPAY56B86nuxdZnrv2EP%2BrdQ%40mail.gmail.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/CA%2BWjgXMLEmHKdtAWBA%2BsO7zPD9XGAggUPVXsU5beqg%3Dwb2Ax5Q%40mail.gmail.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/CAE6v7ofhzu7D2wv3UzK8G4vybKH3pM%3DYWK3fZFzo5Cewk7LUeA%40mail.gmail.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/CA%2BWjgXO8WC7rEHM5nPSumuTjt9f7nZ14B0Z1HuaM1i3%3DnUjmwQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment