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

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日

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法