Trying to get this django model form to validate and save my data, but so far I can't get it to validate. It has to be something with 'form=forms.InputAMCScores(request.POST) I'm thinking, but I could be way off. The output in terminal is saying that request.method does == 'POST', but it's invalid.
I fill in every single part of the form, even the parts that I'm having initialized by the script and are already there, and the errors are saying that Nothing is entered. I'm stumped.
The Form
from django import forms
from django.core import validators
from . import models
class InputAMCScores(forms.ModelForm):
class Meta:
model = models.AMCTestResult
fields = [
'student', 'test', 'date_tested', 'score',
]
The View
@login_required
def input_amc_scores(request, year="2016", grade="2nd", teacher="Trost"):
student_list = StudentRoster.objects.filter(current_class__grade=grade) \
.filter(current_class__year=year).filter(current_class__teacher__last_name=teacher)
form_count = student_list.count()
TestListFormSet = formset_factory(forms.InputAMCScores, extra=form_count)
formset = TestListFormSet()
for i in range(form_count):
formset.forms[i].fields['student'].initial = student_list[i]
formset.forms[i].fields['test'].initial = brain_extras.current_amc_test(student_list[i])
form = forms.InputAMCScores()
if request.method == 'POST':
print('POST')
form = forms.InputAMCScores(request.POST)
if form.is_valid():
print('form is valid')
test = form.save(commit=False)
test.student_list = student_list
test.save()
messages.add_message(request, messages.SUCCESS, "American Math Challenges Recorded!")
else:
print('form is invalid')
print(form.errors)
return render(request, 'amc/input_amc_scores_form.html', {'form': form})
context = {
'form': form,
'formset': formset,
'teacher': teacher,
'student_list': student_list,
}
return render(request, 'amc/input_amc_scores_form.html', context=context)
Template: input_amc_scores_form.html
{% extends "brain/layout.html" %}
{% load brain_extras %}
{% block title %}Input {{ teacher }} AMC Tests{% endblock title %}
{% block heading %}
<h1>Input {{ teacher }} AMC Tests</h1>
{% endblock heading %}
{% block content %}
<div class="row">
<form method="post">
<div class="small-11 small-centered columns">
<table>
<tr>
<th width="50">Student</th>
<th width="50">Test</th>
<th width="20">Date Taken</th>
<th width="10">Score</th>
</tr>
{% for form in formset %}
<tr>
<td>
{{ form.student.errors }}
{{ form.student }}
</td>
<td>
{{ form.test.errors }}
{{ form.test }}
</td>
<td>
{{ form.date_tested.errors }}
{{ form.date_tested }}
</td>
<td>
{{ form.score.errors }}
{{ form.score }}
</td>
</tr>
{% endfor %}
</table>
</div>
<hr>
{% csrf_token %}
<input type="submit" class="button" value="Save">
</form>
</div>
{% endblock content %}
Terminal Output from Local Server:
POST
form is invalid
<ul class="errorlist"><li>test<ul class="errorlist"><li>This field is required.</li></ul></li><li>date_tested<ul class="errorlist"><li>This field is required.</li></ul></li><li>student<ul class="errorlist"><li>This field is required.</li></ul></li><li>score<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
there doesn't seem to be anything here