Rochak Bhattarai
Rochak Bhattarai

Reputation: 101

'str' object has no attribute 'makefile'

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'

Upvotes: 8

Views: 8582

Answers (6)

RAJU MIA
RAJU MIA

Reputation: 111

step: go to views.py change line with add this line.

change line: from http.client import HTTPResponse
add this Line: from django.shortcuts import HttpResponse

Upvotes: 1

Rathish Kumar B
Rathish Kumar B

Reputation: 1422

Import the HttpResponse class from django.http.response module

from django.http.response import HttpResponse
    
def index(request):
    return HttpResponse("Rendering string content")

Note: In some editors, when you add HttpResponse without importing it from the Django, it automatically imports the similarly named module from http.client (from http.client import HTTPResponse)

Upvotes: 1

RAJU MIA
RAJU MIA

Reputation: 111

There's a little mistake. Change the line:

return HTTPResponse("Hello Rochak!")

with

return HttpResponse("Hello Rochak!")

And include this line to the top part as well:

from django.shortcuts import HttpResponse

Note: note that HTTPResponse will be HttpResponse

Upvotes: 9

Gebre Ajax
Gebre Ajax

Reputation: 11

this is due to the capital of "TTP" in the return response - HTTPResponse but the correct one is the small letter of ttp like this:HttpResponse

Upvotes: 1

AKX
AKX

Reputation: 169378

You're returning a http.client.HTTPResponse.

You need to return a django.http.HttpResponse.

Fix your import.

Upvotes: 20

Rochak Bhattarai
Rochak Bhattarai

Reputation: 101

   #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")

Upvotes: 1

Related Questions