I have been trying to implement dynamic forms with a given number of fields and rendering these with crispy forms. However, the following errors are coming when rendering the page:
Template error:
In template C:\Users\USER14\Documents\testprep\lib\site-packages\crispy_forms\templates\bootstrap4\errors.html, error at line 1
'int' object has no attribute 'get'
1 : {% if form.non_field_errors %}
2 : <div class="alert alert-block alert-danger">
3 : {% if form_error_title %}<h4 class="alert-heading">{{ form_error_title }}</h4>{% endif %}
4 : <ul class="m-0">
5 : {{ form.non_field_errors|unordered_list }}
6 : </ul>
7 : </div>
8 : {% endif %}
9 :
Traceback (most recent call last):
File "C:\Users\USER14\Documents\testprep\lib\site-packages\django\forms\forms.py", line 304, in non_field_errors
return self.errors.get(NON_FIELD_ERRORS, self.error_class(error_class='nonfield'))
File "C:\Users\USER14\Documents\testprep\lib\site-packages\django\forms\forms.py", line 170, in errors
self.full_clean()
File "C:\Users\USER14\Documents\testprep\lib\site-packages\django\forms\forms.py", line 372, in full_clean
self._clean_fields()
File "C:\Users\USER14\Documents\testprep\lib\site-packages\django\forms\forms.py", line 384, in _clean_fields
value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
File "C:\Users\USER14\Documents\testprep\lib\site-packages\django\forms\widgets.py", line 657, in value_from_datadict
getter = data.get
Exception Type: AttributeError at /upload/1/SingleCorrect/10/
Exception Value: 'int' object has no attribute 'get'
What exactly prompts this error?
Following are the code snippets I am using.
views.py:
def uploadQP(request,qp,typeofq,numberOfquestion):
if request.method == 'POST':
form=UploadQuestions(repetitions=numberOfquestion,data=request.POST,files=request.FILES)
if form.is_valid():
if typeofq=='SingleCorrect':
for i in range(numberOfquestion):
files=SingleIntegerType()
files.QuestionPaper=get_object_or_404(QuestionPaper,pk=qp)
files.question=request.FILES['question_%d' % i]
files.correct_answer=request.POST['correct_answer_%d' % i]
files.QuestionNumber=request.POST['question_number_%d' % i]
files.save()
return redirect('create_q')
elif request.method=='GET':
form=UploadQuestions(numberOfquestion)
return render(request, 'upload_q.html',{'form':form})
forms.py
class UploadQuestions(forms.Form):
def __init__(self, repetitions, *args, **kwargs):
super(UploadQuestions, self).__init__(*args, **kwargs)
for i in xrange(repetitions):
self.fields['question_number_%d' % i]=forms.IntegerField(label="Enter question number ",widget=forms.NumberInput(attrs={'class': 'form-control', 'placeholder': '1'}))
self.fields['question_%d' % i] = forms.ImageField(label="Select the Image")
self.fields['correct_answer_%d' % i] = forms.ChoiceField(label="Select the correct Option",initial=('A','A'),choices=SINGLE_CORRECT_OPTIONS, widget=forms.Select(attrs={'class': 'custom-select category', 'required': 'true'}))
models.py
class SingleIntegerType(models.Model):
question=models.ImageField(upload_to=create_path)
correct_answer=models.CharField(max_length=10, choices=SINGLE_CORRECT_OPTIONS)
QuestionNumber = models.IntegerField(null=True)
QuestionPaper=models.ForeignKey(QuestionPaper, on_delete=models.DO_NOTHING)
def __str__(self):
return str(self.correct_answer)
Edit: The code has been corrected with the help of the suggestion in the comment.
The code snippet in views.py has been corrected with the help of the comments. The current snippets present a workaround to use a dynamic number of form fields, besides using formsets.