0

I have the following form input in my template that displays questions from the database.

<input type="text" name="qus[{{ question.id }}]" id="qus" value="{{ question.quest }}" />

I created my Django form with:

class QuestionForm(forms.Form):
      qus = SimpleArrayField(forms.CharField(max_length = 50))

Each time I submit the form, it does'nt work. It seems the form is not valid

 MyQuestionForm = QuestionForm(request.POST)
 if request.method == "POST" and MyQuestionForm.is_valid():
     

How can I create the form properly and loop through request.POST['qus'] to retrieve all the values when I submit the form? Please help.

4
  • Does this answer your question? Django array value in a form Commented May 1, 2022 at 8:00
  • Thanks Shayan, but I don't think this help. This is the html array input field: <input type="text" name="qus[{{ question.id }}]" id="qus" value="{{ question.quest }}" /> which means it will have array variable such as qus[1], qus[2], qus[3] etc. How do I retrieve the values in the array in Django? Commented May 1, 2022 at 10:41
  • Well it depends on your queryset and this is not the exact answer but can give some hints. Commented May 1, 2022 at 11:04
  • Thank you. I rename the input field to qus[] instead of the passing value to is as in qus[{{ question.id }}]. I got it working with qus[]. Commented May 1, 2022 at 11:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.