I am using a Formset to generate a set of text entries based on user input and then POST the data when the use selects submit. However, the data does not appear to POST, instead I get an error:
<ul class="errorlist nonfield"><li>(Hidden field TOTAL_FORMS) This field is required.</li><li>(Hidden field INITIAL_FORMS) This field is required.</li></ul>
<div><input type="hidden" name="form-TOTAL_FORMS" id="id_form-TOTAL_FORMS"><input type="hidden" name="form-INITIAL_FORMS" id="id_form-INITIAL_FORMS"><input type="hidden" name="form-MIN_NUM_FORMS" id="id_form-MIN_NUM_FORMS"><input type="hidden" name="form-MAX_NUM_FORMS" id="id_form-MAX_NUM_FORMS"></div>
As far as I can tell I have no hidden fields defined in my models:
Models
class Dashboard(models.Model):
dashboard = models.CharField(max_length=50)
company = models.CharField(max_length=150, default="None")
def __str__(self):
return f"Company: {self.company} Dashboard: {self.dashboard}"
class Objectives(models.Model):
dashboard=models.ForeignKey(Dashboard, on_delete=models.CASCADE)
objective_row = models.IntegerField()
objective_num = models.IntegerField()
class Objective_Colors(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
objective = models.ForeignKey(Objectives, on_delete=models.CASCADE)
color=models.CharField(max_length=10)
value=models.IntegerField()
timestamp= models.TimeField(auto_now = True)
def __str__(self):
return f"{self.color} at {self.timestamp}"
class ObjectiveText(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
objective = models.ForeignKey(Objectives, on_delete=models.CASCADE)
objective_text = models.CharField(max_length=1000, blank = True)
timestamp= models.TimeField(auto_now = True)
Forms
ObjectiveTextFormSet = modelformset_factory(ObjectiveText, fields = ["objective_text", "objective"],
extra = 10, widgets={'objective_text': Textarea(attrs={'cols': 15, 'rows': 5})} )
class LoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)
class LogoutForm(forms.Form):
username = forms.CharField()
views
def home(request):
# Create unique prefixes for each element
formset_list = [
ObjectiveTextFormSet
(prefix=f'row-{i}')
for i in range(6)
]
# Add the formset to context dictionary
context = {
'objectives': [1,2,3,4,5,6,7,8],
'formset_list': formset_list,
}
if request.method == "POST":
form = ObjectiveTextFormSet(request.POST)
print(form)
if form.is_valid():
print("Valid")
objectives = form.save()
# objectivetext = objectives_form.cleaned_data['objective_text']
# objectivenum = objectives_form.cleaned_data['objective_num']
return render(request, 'ISO22301/home.html', context)
else:
return render(request, "ISO22301/home.html", context)
template
{% load static %}
<!DOCTYPE html>
<html lang = "en">
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
<title>Project Management</title>
<h1>Strategy Map</h1>
<form method="POST" action="{% url "home" %}" enctype="multipart/form-data">
{% csrf_token %}
<table>
<head>
<th>Project</th>
{% for n in objectives %}
<th>Objective</th>
{% endfor %}
<th>Outcome</th>
</head>
<body>
{{ formset.management_form }}
{% for formset in formset_list %}
<tr>
{% for form in formset %}
<td> {{ form.objective_text }} </td>
{% endfor %}
</tr>
{% endfor %}
</table>
<div>
<input type="submit" value="Update Objectives">
</div>
</form>
</body>
</html>
DB (SQLLite)