微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Django - 带有动态字段的脆脆表单的模板渲染过程中出错:“int”对象没有属性“get”

如何解决Django - 带有动态字段的脆脆表单的模板渲染过程中出错:“int”对象没有属性“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 中的代码片段已在注释的帮助下得到纠正。当前代码段提供了一种解决方法,除了使用表单集之外,还可以使用动态数量的表单字段。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?