weixin_44460259 2023-03-26 16:14 采纳率: 100%
浏览 14
已结题

Django didn't return an HttpResponse object问题

急救,大老板们指点~
运行的时候直接报错,The view bookstore.views.update_book didn't return an HttpResponse object. It returned None instead.
**尝试按照网上的方案去修改缩进量,但是无效。__**

```python
def update_book(request, book_id):
    try:
        book = Book.objects.get(id=book_id, is_active=True)
    except Exception as e:
        print('update book error is %s' % e)
        return HttpResponse('book is not existed')
    if request == 'GET':
        return render(request, 'bookstore/update_book.html', locals())
    elif request == 'POST':
        market_price = request.POST['market_price']
        price = request.POST['price']
        book.price = price
        book.market_price = market_price
        book.save()
        return HttpResponseRedirect('/bookstore/all_book')



template 里面的html 如下:

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>update book</title>
</head>
<body>

<form action="/bookstore/update_book/{{ book.id }}" method="POST">
    <p>
        title <input type="text" value="{{ book.title }}" disabled="disabled">
    </p>
    <P>
        pub <input type="text" value="{{ book.pub }}" disabled="disabled" >
    </P>
    <p>
        price <input type="text" name="price" value="{{ book.price }}">
    </p>
    <p>
        market_price <input type="text" name="market_price" value="{{ book.market_price }}" >
    </p>
    <p>
        <input type="submit" value="renew">
    </p>

</form>

</body>
</html>


运行结果及详细报错内容

The view bookstore.views.update_book didn't return an HttpResponse object. It returned None instead.
Request Method: GET
Request URL: http://127.0.0.1:8000/bookstore/update_book/1
Django Version: 4.1.7
Exception Type: ValueError
Exception Value:
The view bookstore.views.update_book didn't return an HttpResponse object. It returned None instead.
Exception Location: D:\JNU\mysite\venv\Lib\site-packages\django\core\handlers\base.py, line 332, in check_response
Raised during: bookstore.views.update_book
Python Executable: D:\JNU\mysite\venv\Scripts\python.exe
Python Version: 3.11.2

  • 写回答

1条回答 默认 最新

  • IT_service_mesh 2023-03-26 16:31
    关注

    参考GPT和自己的思路:根据你提供的代码,可以看出在函数中有一个代码块缺少返回HttpResponse的语句,导致视图在某些情况下没有返回任何响应。具体来说,update_book函数中的第一个if request == 'GET'条件块中没有将HttpResponse对象返回,所以当请求为GET时会返回None。

    要修复这个问题,只需在该条件块中添加一个HttpResponse对象返回即可。下面是修改后的代码示例:

    def update_book(request, book_id):
        try:
            book = Book.objects.get(id=book_id, is_active=True)
        except Exception as e:
            print('update book error is %s' % e)
            return HttpResponse('book is not existed')
        
        if request.method == 'GET':
            return render(request, 'bookstore/update_book.html', locals())
        elif request.method == 'POST':
            market_price = request.POST['market_price']
            price = request.POST['price']
            book.price = price
            book.market_price = market_price
            book.save()
            return HttpResponseRedirect('/bookstore/all_book')
    

    注意,我还在第一个条件块中修正了if request == 'GET'if request.method == 'GET' ,因为你需要比较请求的方法类型而不是它的值。

    希望这可以帮助您解决问题。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月3日
  • 已采纳回答 3月26日
  • 创建了问题 3月26日

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备