Django - 在为具有动态字段的脆皮表单呈现模板时出错:'int' 对象没有属性 'get'

Django - Error during template rendering for crispy forms with dynamic fields : 'int' object has no attribute 'get'

我一直在尝试用给定数量的字段实现动态表单,并用清晰的表单呈现这些表单。但是,渲染页面时出现以下错误:

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'

究竟是什么提示了这个错误?

以下是我正在使用的代码片段。

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)

编辑:已根据评论中的建议更正代码。

views.py 中的代码片段已在评论的帮助下得到更正。当前的代码片段提供了一种变通方法,除了使用表单集之外,还可以使用动态数量的表单字段。