try: Publisher_obj = models.Publisher.objects.all() Author_obj = models.Author.objects.all() BookType_obj = models.BookType.objects.all() if request.method == 'POST': back_dic = {"code": 200, 'msg': ''} bookface = request.FILES.get('bookface') book_obj = dict(request.POST) print(book_obj) price = Decimal(request.POST.get('price')) ISBN = request.POST.get('ISBN') obj = models.Book.objects.filter(ISBN=ISBN).first() if not obj: book = models.Book.objects.create(name=book_obj['name'], face=bookface, ISBN=book_obj['ISBN'], price=price, pub_date=book_obj['pub_date'], book_number=book_obj['number'], content=0, info=book_obj['info']) book.book_type.set(book_obj['booktype']) book.author.set(book_obj['author']) book.publisher.set(book_obj['publisher']) book.save() back_dic['url'] = 'admin_book' else: back_dic['code'] = 303 back_dic['msg'] = '错误' return JsonResponse(back_dic) return render(request, 'admin_book_add.html', locals()) except Exception: traceback.print_exc()
时间: 2024-01-27 19:06:26 浏览: 121
在这段代码中,如果出现了异常,它并没有返回一个 `HttpResponse` 对象,而是直接打印了异常信息。这可能会导致在发生异常时,返回一个空值(`None`),从而引发 `ValueError` 错误。
为了解决这个问题,你可以在 `except` 代码块中添加一个返回语句,返回一个 `HttpResponse` 对象,例如:
```
except Exception as e:
traceback.print_exc()
return HttpResponse('An error occurred: {}'.format(e))
```
这样,当出现异常时,它将返回一个包含异常信息的 `HttpResponse` 对象,而不是空值(`None`)。当然,你可以根据你的具体需求来定制返回的内容。
希望这能帮助你解决问题。
阅读全文