Django验证错误没有提供异常

时间:2013-04-13 01:07:00

标签: django validation exception

我正在通过jquery发帖到我的后端。我首先看看是否已经作出承诺。如果没有,我创建一个。

@csrf_exempt
def pledgeReceive(request):
fname = request.POST.get('fname', '')
lname = request.POST.get('lname', '')
participantName = request.POST.get('participantName', '')
participantID = request.POST.get('participantID', '')
ppl = request.POST.get('ppl', '')
maxi = request.POST.get('maxi', '')

sponsor = fname + ' ' + lname
participant_obj = Participant.objects.get(pk = participantID)

try:
    pledge = Pledge.objects.get(sponsor = sponsor, participant = participant_obj, pledge_amount = ppl, max_pledge_amount = maxi, datetime = datetime.now())
except Pledge.DoesNotExist:
    pledge = Pledge(sponsor = sponsor, participant = participant_obj, pledge_amount = ppl, max_pledge_amount = maxi, datetime = datetime.now())
    pledge.save()

response = HttpResponse()
response.content = serialized_obj = serializers.serialize('json', [ pledge, ])
response['Content-Type'] = 'application/json'
return response

我收到500内部服务器错误。使用以下回溯:

ValidationError at /pledgeReceive/
No exception supplied
    Traceback:
File "/home/vtrelayc/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/vtrelayc/lib/python2.6/site-packages/django/views/decorators/csrf.py" in wrapped_view
  77.         return view_func(*args, **kwargs)
File "/home/vtrelayc/projects/relay/relayapp/views.py" in pledgeReceive
  461.  pledge.save()
File "/home/vtrelayc/lib/python2.6/site-packages/django/db/models/base.py" in save
  463.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/home/vtrelayc/lib/python2.6/site-packages/django/db/models/base.py" in save_base
  551.                 result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/home/vtrelayc/lib/python2.6/site-packages/django/db/models/manager.py" in _insert
  203.         return insert_query(self.model, objs, fields, **kwargs)
File "/home/vtrelayc/lib/python2.6/site-packages/django/db/models/query.py" in insert_query
  1593.     return query.get_compiler(using=using).execute_sql(return_id)
File "/home/vtrelayc/lib/python2.6/site-packages/django/db/models/sql/compiler.py" in execute_sql
  911.         for sql, params in self.as_sql():
File "/home/vtrelayc/lib/python2.6/site-packages/django/db/models/sql/compiler.py" in as_sql
  872.                 for obj in self.query.objs
File "/home/vtrelayc/lib/python2.6/site-packages/django/db/models/fields/__init__.py" in get_db_prep_save
  873.         return connection.ops.value_to_db_decimal(self.to_python(value),
File "/home/vtrelayc/lib/python2.6/site-packages/django/db/models/fields/__init__.py" in to_python
  850.             raise exceptions.ValidationError(msg)

Exception Type: ValidationError at /pledgeReceive/

我使用except Pledge.DoesNotExist:提供了例外?

2 个答案:

答案 0 :(得分:1)

为什么不使用get_or_create?它是您尝试做的快捷方式。

pledge, created = Pledge.objects.get_or_create(sponsor = sponsor, participant = participant_obj, pledge_amount = ppl, max_pledge_amount = maxi, datetime = datetime.now())

现在你有一个pledge对象和一个布尔值created,它可以让你知道对象是创建还是被提取。

除此之外,错误可能是由于您只是获取GET参数而不将其转换为Pledge对象所期望的类型。即使大多数字段可能不期望字符串,它们也会作为字符串传递。也许使用表单(因为它内置验证/类型转换)或手动将您的GET参数转换为正确的类型。

答案 1 :(得分:1)

当数据表单或模型字段验证失败时会引发

ValidationError异常,但由于您未使用form.is_valid()或验证输入值的任何验证程序,这将导致No Exception Supplied。即使你的Pledge.DoesNotExist也无法捕获错误,因为它只检查何时找不到查询的给定参数的对象。

根据您的代码,我发现了无效的值。

ppl = request.POST.get('ppl', '')
maxi = request.POST.get('maxi', '')

如果我没错,上面的代码会传递一个整数。但你不知道你传递的字符串不是int。一定是,

ppl = int(request.POST.get('ppl', 0))
maxi = int(request.POST.get('maxi', 0))

我不知道您的模型代码模式,如果您传递foreignkey或date的值,您还必须检查它,因为它们非常敏感。您不必担心CharField,因为除非有必要,否则他们可以处理它。但是如果你无法处理这个错误,我建议你使用form