Thanks. I’ve gotten used to the first being 0 in python that I default to that.
I removed objective from the formset:
ObjectiveTextFormSet = modelformset_factory(ObjectiveText, fields = ["objective_text"], extra = 0, widgets={'objective_text': Textarea(attrs={'cols': 15, 'rows': 5})} )
I’ve tried to get error messages but am not sure if I am or not. The terminal, in response to my code, yields:
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[21/Oct/2024 10:55:13] "GET /ISO22301/home/ HTTP/1.1" 200 15718
[21/Oct/2024 10:55:13] "GET /static/css/styles.css HTTP/1.1" 304 0
Formset errors: []
Not Vald
{'formset_list': <ObjectiveTextFormFormSet: bound=True valid=False total_forms=0>}
[21/Oct/2024 10:55:19] "POST /ISO22301/data/ HTTP/1.1" 200 638
[21/Oct/2024 10:55:19] "GET /static/css/styles.css HTTP/1.1" 304 0
So it looks like it is using GET to render the initial template, and after I enter data using POST to get the view. I split the home view into one that renders the template as a blank and then a separate view, data, to handle the POST data, just in case having them in the same view was causing the issue by creating a fresh, blank formset when called by POST.
Looking at the print data, the error message is a blank dictionary, but it is not executing the
if request.method == "POST":
loop.
views (probably don’t need the duplicate formset_list = ObjectiveTextFormSet(request.POST):
def home(request):
# Create unique prefixes for each element
formset_list = [
ObjectiveTextFormSet
(prefix=f'row-{i}',
queryset=ObjectiveText.objects.filter(
objective__objective_row=i
).order_by('objective__objective_num')
)
for i in range(1,7)
]
objheader = [1,2,3,4,5,6,7,8]
# Add the formset to context dictionary
context = {
'formset_list': formset_list,
'objheader': objheader,
}
return render(request, "ISO22301/home.html", context)
def data(request):
context={}
formset_list = ObjectiveTextFormSet(request.POST)
context['formset_list']=formset_list
print("Formset errors:",formset_list.errors)
if request.method == "POST":
formset_list = ObjectiveTextFormSet(request.POST)
for formset in formset_list:
if formset.is_valid():
print("Valid")
formset.save()
return render(request, 'ISO22301/generic.html', context)
else:
print("Formset errors after checkink if valid:",formset_list.errors)
return render(request, 'ISO22301/generic.html', context)
print("Not Vald")
print(context)
return render(request, 'ISO22301/home.html', context)