How to use exception handling in Django view

我只是一个虾纸丫 提交于 2019-12-25 00:28:14

问题


Suppose i have this code

if form.is_valid(): 
   form.save()

Now suppose my form is valid i have exception that foregin key value is linked to more than one column so it will raise exception

Now i want to know is there any way to grab that exception value and pass to jquery via AJAX

Because form is valid so it comes inside the loop but its not able to go after form.save

So how can i program that if exception occurs it get passed to jquery like

if exception
   return HttpResponse(exception)

I get this exception

MultipleObjectsReturned at /manage/Source/create/ get() returned more than one Account -- it returned 3! Lookup parameters were {'account_number': u'121121'}

What type of exception it is


回答1:


MultipleObjectsReturned is the exception.

try:
    #do something
except MultipleObjectsReturned:
    return HttpResponse('MultipleObjectsReturned')

I wouldn't recommend using a bare try/except to catch all exceptions, as you won't know exactly what's wrong.



来源:https://stackoverflow.com/questions/6622149/how-to-use-exception-handling-in-django-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!