天天看點

python 3中try和except 以及 exception的寫法

@require_http_methods(["GET"])

def add_book(request):

    response = {}

    try:

        book = Book(book_name=request.GET.get('book_name'))

        book.save()

        response['msg'] = 'success'

        response['error_num'] = 0

    except  Exception as e:

        response['msg'] = str(e)

        response['error_num'] = 1

    return JsonResponse(response)

更多資訊,參考文章:https://www.cnblogs.com/zhangyingai/p/7097920.html