Home > other >  : AttributeError: 'str' object has no attribute 'resolve' o
: AttributeError: 'str' object has no attribute 'resolve' o

Time:01-18

I am new to Django I am using a mac os system, I am trying to do a simple hello world program in the Django framework

The initial set up and launching works fine, but while creating a helloworld program by creating a new app and deploying it I am getting the below error can someone help how to resolve this issue?

**Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 4.0.1
Python Version: 3.10.1
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
  File "/Users/ushanandhini/Desktop/djngo/.venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Users/ushanandhini/Desktop/djngo/.venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/ushanandhini/Desktop/djngo/hello/views.py", line 8, in home
    return HTTPResponse("hello Nikhil")
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/http/client.py", line 256, in __init__
    self.fp = sock.makefile("rb")
Exception Type: AttributeError at /
Exception Value: 'str' object has no attribute 'makefile'

Thanks, Sid**

CodePudding user response:

There is 2 definitions:

HttpResponse in django

HTTPResponse in python.

You are using the python class with the django init argument style: HTTPResponse("hello Nikhil").

So make sure you use the right import statement AND pay attention to the HTTP and Http:

from django.http import HttpResponse

Just for information

  1. Python Definition:
class HTTPResponse(io.BufferedIOBase):
    def __init__(self, sock, debuglevel=0, method=None, url=None):
  1. Django Definition:
class HttpResponse(HttpResponseBase):
    def __init__(self, content=b'', *args, **kwargs):
  •  Tags:  
  • Related