【问题标题】:'str' object has no attribute 'makefile'“str”对象没有属性“makefile”
【发布时间】:2022-01-26 17:59:51
【问题描述】:
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'

【问题讨论】:

  • 您发布了整个回溯,这很好,但也请发布您的代码

标签: python django


【解决方案1】:

您返回的是http.client.HTTPResponse

您需要返回django.http.HttpResponse

修复您的导入。

【讨论】:

  • 它的拼写有点不同。我相信班级名称是HttpResponse
  • 非常感谢!!只是因为多次导入,我已经麻烦了很长时间!
  • @JohnGordon 很好,已修复。
【解决方案2】:
   #Remove http.client.HTTPResponse from import and follow:
   from django.shortcuts import render
   from django.http import HttpResponse

   # Create your views here.
   def home(request):
        return HttpResponse("Hello bro")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 2016-09-20
    • 2019-03-25
    • 2017-05-13
    • 2014-04-28
    • 2015-05-15
    • 2019-06-20
    相关资源
    最近更新 更多