[Fixed]-How can submit form with array fields and process on view?

1👍

✅

Actually what is posted to you is ans0, ans1, ..., ansN.
So what you want to do is something like this:

answer_fields = [field for field in request.POST if field.startswith('ans')]
for field in answer_fields:
    # Do something with `field`...
    print(request.GET[field])

Additionally, you might want to check that the latter part is numeric, like so:

[field for field in request.POST
 if field.startswith('ans') and field[3:].isnumeric()]

Leave a comment