Milan
Milan

Reputation: 2013

Validation fails on ModelForm

NOTE/UPDATE: This issue was caused by spelling error :(


Hi I'm trying to teach my self Django while making a small Task management app.

I have a Model

class Task(models.Model):
    track = models.ForeignKey(Track, on_delete=models.SET_NULL, null=True)
    title = models.CharField(max_length=100)
    description = models.CharField(max_length=265, blank=True)
    done = models.BooleanField(default=False)

    def __str__(self):
        return self.title

and related ModelForm

class TaskForm(forms.ModelForm):
    class Meta:
        model = Task
        fields = ['track', 'title', 'description', 'done']

When form is posted taskForm.is_valid() is returning False. This is post_task method:

def post_task(request):
    form = TaskForm(request.POST)
    if form.is_valid():
        form.save(commit=True)
    else:
        print(form.errors)
    return HttpResponseRedirect('/')

and the form tag on the page:

     <form action="post_url" mehod="post">
        {% csrf_token %}
        {{ task_form.as_p }}
        <input type="submit" value="Add"/>
     </form>

Even though I've filled in all the data I'm getting validation error, this is the console print:

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

All values have been passed in the request:

[23/Apr/2017 12:34:38] "GET /post_url/?csrfmiddlewaretoken=VqUx3EM9yGFzS88kYRtTWtniaCV8ZukxymylPILlxHBohtfEyhD3epOKOjKNIVCU&track=1&title=testTitle&description=testDescription HTTP/1.1" 302 0

Thanks!

Upvotes: 0

Views: 36

Answers (1)

Thameem
Thameem

Reputation: 3636

a spelling mistake occurs there mehod

<form action="post_url" mehod="post">

change this to

<form action="post_url" method="post">

Upvotes: 2

Related Questions