- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我尝试在 Django 1.8 中使用“add_error”函数。然后我得到错误“没有 add_error 的属性”。提前感谢您的帮助。
views.py
class FinalView(ListView):
context_object_name = 'XXX'
template_name = 'XXX.html'
model = Final
def get_queryset(self):
form = InputForm(self.request.GET)
if form.is_valid():
department = form.cleaned_data['department']
person = form.cleaned_data['person']
if department !="" and person !="":
if Final.objects.filter(department=department,person=person).exists():
queryset=Final.objects.filter(department=department,person=person)
else:
self.add_error(ValidationError('No corresponding data exists')) ------here reports error----
return queryset
return Final.objects.all()
def get_context_data(self,**kwargs):
context["sales"] = self.get_queryset().aggregate(Sum('sales'))
回溯
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\views\generic\base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\views\generic\base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\views\generic\list.py" in get
159. self.object_list = self.get_queryset()
File "C:\Users\user\Desktop\XXX\XXXX\views.py" in get_queryset
70. self.add_error(ValidationError('No corresponding data exists'))
Exception Type: AttributeError at /final/
Exception Value: 'FinalView' object has no attribute 'add_error'
最佳答案
add_error
needs to be applied to a Form ,而不是 ListView。
改变这个:
self.add_error(ValidationError('No corresponding data exists'))
对此:
form.add_error(ValidationError('No corresponding data exists'))
根据关于新异常的评论,add_error
有两个参数:
Form.add_error(field, error)
This method allows adding errors to specific fields from within the Form.clean() method, or from outside the form altogether; for instance from a view.
The
field
argument is the name of the field to which the errors should be added. If its value isNone
the error will be treated as a non-field error as returned byForm.non_field_errors()
.
error
参数可以是一个简单的字符串,或者最好是 ValidationError
的实例。
在您的情况下,由于这与两个领域相关,您可能需要:
form.add_error(None,ValidationError('No corresponding data exists'))
关于python - django-1.8 : "XXXView"object has no attribute 'add_error' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33685604/
您可以通过两种方式抛出验证错误。第一个是 self.add_error()第二个是 raise ValidationError() . 当你使用 self.add_error('field1','de
我有一个像这样的模型序列化器: class MySerializer(ModelSerializer): class Meta: model = MyModel
我尝试在 Django 1.8 中使用“add_error”函数。然后我得到错误“没有 add_error 的属性”。提前感谢您的帮助。 views.py class FinalView(ListVi
我是一名优秀的程序员,十分优秀!