1👍
The prefix
argument is there to prevent naming conflict issues when repeatedly using the same form (FormSets use it) or using forms which happen to have clashing field names – give each Form
a unique prefix
, which will be prepended to each generated field name. Using the Question’s id would be ideal:
return [TextAnswerForm(q,data=post, prefix='q_%s' % q.pk)
for q in survey.questions.all()]
2👍
The reason is probably that the names of the form fields clash.
You could work around that issue, but did you already have a look at the Formsets Documentation?
A formset is a layer of abstraction to working with multiple forms on the same page.
- [Django]-Get minimum of related model date field (django)
- [Django]-How to use IntegerRangeField in Django template
- [Django]-Database localization in Django
1👍
you can’t get from request object, how many form there was in client www page.
It is allowed to use multiple form in html document – but the only difference is that the POST/GET data contains only the fields from submitted form.
So put all your input data in one form and then
The simplest solution is to write something like this in your template
<form action="your_viw" method="post">
{% for q in questions %}
{{q.question}}<input name="q_{{q.id}}" type="text" />
{% endfor %}
</form>
and in your view
def show_questions(request, slug):
survey = get_object_or_404(Survey.objects, slug=slug)
context = {
'survey':survey,
'questions': survey.questions_set,
}
fields=[(int(name[2:]), val) for name, val in request.POST.items() if name.startswith('q_')] # (question id, answer) list
if fields:
#validate fields
# rest of work...
return ...
Sorry if there is some misspelled word.
- [Django]-Extensions and libraries for Django
- [Django]-Django Tastypie include count on many-to-many fields
- [Django]-DjangoUnicodeDecodeError in administration with DEBUG=False
- [Django]-How can I send an email to user on new user addition to django admin site?
- [Django]-How can you drop an HttpRequest connection in Django when TLS/SSL is not being used?