0

我正在通过 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:

4

2 回答 2

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 参数转换为正确的类型。

于 2013-04-13T01:23:18.010 回答
1

ValidationErrorform.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))

我不知道您的模型代码模式,如果您传递外键或日期的值,您还必须检查,因为它们非常敏感。您不必担心 CharField,因为除非需要,他们可以处理它。但我建议你使用form,如果你不能处理这个错误。

于 2013-04-13T02:19:13.910 回答