Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

: AttributeError: 'str' object has no attribute 'resolve' o

Tags:

python

django

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**

like image 577
Sidh Avatar asked Feb 02 '26 15:02

Sidh


1 Answers

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):
like image 60
Razenstein Avatar answered Feb 04 '26 05:02

Razenstein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!