“str”对象没有属性“makefile”

Roc*_*rai 6 python django

Traceback (most recent call last): 
File "C:\Users\Rochak.virtualenvs\GFG\GFG\lib\site-packages\django\core\handlers\exception.py", line 47, in inner 
  response = get_response(request)
File "C:\Users\Rochak.virtualenvs\GFG\GFG\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
  response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Rochak.virtualenvs\GFG\GFG\Scripts\authentication\views.py", line 7, in home
  return HTTPResponse("Hello Rochak!")
File "C:\Program Files\Python310\lib\http\client.py", line 256, in init
  self.fp = sock.makefile("rb")

Exception Type: AttributeError at /authentication/hello/
Exception Value: 'str' object has no attribute 'makefile'
Run Code Online (Sandbox Code Playgroud)

AKX*_*AKX 15

您正在返回一个http.client.HTTPResponse.

您需要返回一个django.http.HttpResponse.

修复您的导入。


小智 8

有一个小错误。更改线路:

return HTTPResponse("Hello Rochak!")
Run Code Online (Sandbox Code Playgroud)

return HttpResponse("Hello Rochak!")
Run Code Online (Sandbox Code Playgroud)

并将这一行也包含在顶部:

from django.shortcuts import HttpResponse
Run Code Online (Sandbox Code Playgroud)

注意:请注意,这HTTPResponse将是HttpResponse