Django - always getting False in form.is_valid()

1,631 views
Skip to first unread message

Sipum Mishra

unread,
Apr 19, 2019, 7:05:31 AM4/19/19
to Django users
Hi All,

I am always getting - form.is_valid returning False. kindly check where I am doing wrong.
please find below code. 


views.py
-----------
def home(request):

if request.method == 'POST':
form = ListForm(request.POST or None)
print(form.is_valid(), "-->",request.POST['Item'])
print(form.errors)
print(form)
if form.is_valid():
form.save()
all_items = List.objects.all
messages.success(request, ('Item has been Added to the List!'))
return render(request,'home.html', {'all_items' : all_items})
else:
print("deba-->",request.POST)
return HttpResponse("Form is invalid!")
else:
all_items = List.objects.all
return render(request,'home.html', {'all_items' : all_items})

-------------
form.py
-------------

class ListForm(forms.ModelForm):
class Meta:
model = List
fields = ["item", "completed"]


--------------------
model.py
---------------------

class List(models.Model):
item = models.CharField(max_length=200)
completed = models.BooleanField(default=False)


def __str__(self):
return self.item + '|' + str(self.completed)

vineeth sagar

unread,
Apr 19, 2019, 9:34:52 AM4/19/19
to django...@googlegroups.com
can you please post, request.POST output?

--
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/4ca5e82d-f34b-4ab4-8f82-5be0dcdebecc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sipum Mishra

unread,
Apr 20, 2019, 1:33:29 PM4/20/19
to django...@googlegroups.com
Hi Vineeth,

please find below is the output -> 

<QueryDict: {'csrfmiddlewaretoken': ['8V0Or1mvx2gtOa5fuYsdmAJ1o7SpGOcbSYulW0WMACHbrBhn8tOI0SO1z65MzXGx'], 'Item': ['add tesing']}>

Sipum Mishra

unread,
Apr 20, 2019, 1:35:26 PM4/20/19
to django...@googlegroups.com
Hi Vineeth,

when I am trying to add 'add testing' through html form for todo list app getting this above form.is_valid() always False.

John Bagiliko

unread,
Apr 20, 2019, 2:36:46 PM4/20/19
to django...@googlegroups.com
Drop the html codes here please. Let one try it actually. It's difficult to debug like this. 

Sipum Mishra

unread,
Apr 20, 2019, 2:42:13 PM4/20/19
to Django users
Hi John, Thanks alot for your reply.
please find below code of my base.html.

{% load static %}

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <link rel="stylesheet" href="{% static 'css/my_css.css' %}">

    <title>TODO LIST</title>
  </head>
  <body>

    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  <a class="navbar-brand" href="{% url 'home' %}"> TO-DO List</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      
      
    </ul>
    <form class="form-inline my-2 my-lg-0" method="POST">
    {% csrf_token %}
      <input class="form-control mr-sm-2" type="search" placeholder="Add Item To List" aria-label="Search" name = 'Item'>
      <button class="btn btn-outline-secondary my-2 my-sm-0" type="submit">Add Item</button>
    </form>
  </div>
</nav>
    </br>
    <div class="container">
      {% block content %}
      {% endblock %}
    </div>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </body>
</html>

Jani Tiainen

unread,
Apr 21, 2019, 7:13:10 AM4/21/19
to django...@googlegroups.com
completed is a required field and you're not passing any value with it. If you print form.errors you would should see that it complains about "completed" "this field is required"

Note that providing default value in a model is not same as making field optional. 

--
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/4ca5e82d-f34b-4ab4-8f82-5be0dcdebecc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Jani Tiainen
Software wizard


Always open for short term jobs or contracts to work with Django.

Sipum Mishra

unread,
Apr 21, 2019, 7:54:11 AM4/21/19
to django...@googlegroups.com
Hi Jani,
form.errors gives error as -

<ul class="errorlist"><li>item<ul class="errorlist"><li>This field is required.<
/li></ul></li></ul>

can u plz tell me where i did wrong?

Sipum Mishra

unread,
Apr 21, 2019, 7:57:59 AM4/21/19
to django...@googlegroups.com
Jani, Do you mean I need to change completed field in model like as below ?

completed = models.BooleanField(default=False,required=False)

Sipum Mishra

unread,
Apr 21, 2019, 8:02:27 AM4/21/19
to django...@googlegroups.com
if i do changes as above in model then it throws error like -

 class List(models.Model):
  File "F:\django\to_do_list\todo\models.py", line 6, in List
    completed = models.BooleanField(required=False)
TypeError: __init__() got an unexpected keyword argument 'required'

Jani Tiainen

unread,
Apr 21, 2019, 8:06:08 AM4/21/19
to django...@googlegroups.com
Hi,

required attribute works in forms, not in models. For models fields that are not required in forms you need to use blank=True.

Though that's not the right solution.

Why are you including "completed" field in a form (which you validate for user input) if you are not going to have such a input (checkbox) in a form?



For more options, visit https://groups.google.com/d/optout.

Sipum Mishra

unread,
Apr 21, 2019, 8:17:38 AM4/21/19
to django...@googlegroups.com
Hi Jani, the above solution is not working, still having the same problem.

and that completed field is used later for cross_off and uncross the item in list.

Reply all
Reply to author
Forward
0 new messages