django form.is_valid() always returns false

Abhinav Nair

My is valid function in my view always seems to be returning false even though the post seems to be sending the right data (I think). I'm pretty new to django and python and I'm using the Django Docs as a guide. I am also trying to implement my form using the django form example in dajax (I have already installed and used dajax successfully in the same project). My other Dajax methods are get methods and I'm sure they don't interfere with my post.

Each time I hit post, the "form is not valid" error alert I have shows up and I am not sure why it doesn't enter the if block.

I have read similar questions on here and have double checked everything I could think of. I would be so grateful if anyone could help point out where the problem is. I am pasting the required code below.

forms.py

class BedSelectForm(forms.Form):
Branch = forms.ModelChoiceField(
    label = u'Branch',
    queryset = Result.objects.values_list('branch', flat =True).distinct(),
    empty_label = 'Not Specified',
    widget = forms.Select(attrs = {'onchange' : "Dajaxice.modmap.updatecomboE(Dajax.process, {'optionB':this.value})"})
    )
Env = forms.ModelChoiceField(
    label = u'Environment',
    queryset = Result.objects.values_list('environment', flat =True).distinct(),
    empty_label = 'Not Specified',
    widget = forms.Select(attrs = {'onchange' : "Dajaxice.modmap.updatecomboD(Dajax.process, {'optionE':this.value})"})
    )
Disc = forms.ModelChoiceField(
    label = u'Discipline',
    queryset = Result.objects.values_list('discipline', flat =True).distinct(),
    empty_label = 'Not Specified'

    )

template.html

<form action="" method="post" id = "select_form">
        <div style = "color :white" class="field_wrapper">
             {{ form.as_p }}
        </div>        
        <input type="button" value="Display Table" onclick="send_form();"/>
</form>

<script type="text/javascript">
    function send_form(){
        Dajaxice.modmap.dispTable(Dajax.process,{'form':$('#select_form').serialize(true)});
    }
 </script>

ajax.py

@dajaxice_register
def dispTable(request, form):
    dajax = Dajax()
    form = BedSelectForm(deserialize_form(form))

    if form.is_valid():
        dajax.remove_css_class('#select_form input', 'error')
        dajax.alert("Form is_valid(), your username is: %s" % form.cleaned_data.get('Branch'))
    else:
        dajax.remove_css_class('#select_form input', 'error')
        for error in form.errors:
            dajax.add_css_class('#id_%s' % error, 'error')
        dajax.alert("Form is_notvalid()")

    return dajax.json()

This is what my post looks like..

argv    {"form":"Branch=Master&Env=ProdCoursera&Disc=ChemistryMOOC"}
Abhinav Nair

I found the solution to my problem. I was trying to pick tuples using a ModelChoiceField which is a very hacky way of doing things because a ModelChoiceField expects model objects not arbitrary strings.

I replaced my forms with Choice Fields and populated them by overriding the init function. Below are the changes I made to my form for reference. I hope this helps someone!

class BedSelectForm(forms.Form):
Branch = forms.ChoiceField(
    choices = [],)

Env = forms.ChoiceField(
    choices = [],)

Disc = forms.ChoiceField(
    choices = [],)

def __init__(self, *args, **kwargs):
    super(BedSelectForm, self).__init__(*args, **kwargs)
    self.fields['Branch'].choices = [(x.pk, x.branch) for x in Result.objects.distinct()]
    self.fields['Env'].choices = [(x.pk, x.environment) for x in Result.objects.distinct()]
    self.fields['Disc'].choices = [(x.pk, x.discipline) for x in Result.objects.distinct()]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

django is_valid returns false for model form even though all fields have a value

From Dev

Rest framework serializer always returns False for is_valid

From Dev

Django form is valid() returns false

From Dev

My form's is_valid method returns false

From Dev

Django 3: is_valid() is always false using FileField and FloatField

From Dev

form.is_valid(): always returns false

From Dev

Django form.is_valid() always false

From Dev

form.is_valid() returns false (django)

From Dev

django form.is_valid() returns 0 always in case of file upload

From Dev

Django model form is_valid() in specific database

From Dev

Django form with checkbox is always False

From Dev

spring form validation BindingResult always returns false

From Dev

Django: difference between is_valid and form_valid

From Dev

form.is_valid always return false

From Dev

is_valid() throw exception when a simple form is invalid in django 1.6

From Dev

Django form unittest with ChoiceField and MultipleChoiceField failing is_valid()

From Dev

Django form posts data with no errors, but does not register as 'is_valid'

From Dev

Symfony2 REST API, always returns false validating the form

From Dev

codeigniter form validation always returns false even when rules met

From Dev

form.isValid() always return false in custom registration form django

From Dev

Django REST serializer.is_valid() returns False

From Dev

django: form.is_valid always giving error

From Dev

Boolean always returns false

From Dev

setProcessDefaultNetwork always returns false

From Dev

isEqualToString always returns False

From Dev

condition always returns false

From Dev

Regex that always returns false

From Dev

StrPos always returns False?

From Dev

TrySetWallpaperImageAsync() always returns false

Related Related

  1. 1

    django is_valid returns false for model form even though all fields have a value

  2. 2

    Rest framework serializer always returns False for is_valid

  3. 3

    Django form is valid() returns false

  4. 4

    My form's is_valid method returns false

  5. 5

    Django 3: is_valid() is always false using FileField and FloatField

  6. 6

    form.is_valid(): always returns false

  7. 7

    Django form.is_valid() always false

  8. 8

    form.is_valid() returns false (django)

  9. 9

    django form.is_valid() returns 0 always in case of file upload

  10. 10

    Django model form is_valid() in specific database

  11. 11

    Django form with checkbox is always False

  12. 12

    spring form validation BindingResult always returns false

  13. 13

    Django: difference between is_valid and form_valid

  14. 14

    form.is_valid always return false

  15. 15

    is_valid() throw exception when a simple form is invalid in django 1.6

  16. 16

    Django form unittest with ChoiceField and MultipleChoiceField failing is_valid()

  17. 17

    Django form posts data with no errors, but does not register as 'is_valid'

  18. 18

    Symfony2 REST API, always returns false validating the form

  19. 19

    codeigniter form validation always returns false even when rules met

  20. 20

    form.isValid() always return false in custom registration form django

  21. 21

    Django REST serializer.is_valid() returns False

  22. 22

    django: form.is_valid always giving error

  23. 23

    Boolean always returns false

  24. 24

    setProcessDefaultNetwork always returns false

  25. 25

    isEqualToString always returns False

  26. 26

    condition always returns false

  27. 27

    Regex that always returns false

  28. 28

    StrPos always returns False?

  29. 29

    TrySetWallpaperImageAsync() always returns false

HotTag

Archive