开发者

How to use exception handling in Django view

开发者 https://www.devze.com 2023-03-18 00:35 出处:网络
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

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

MultipleObje开发者_Go百科ctsReturned 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号