1

I tried to setup hello world project but it gives me this error:

TypeError at /hello/
__init__() takes 1 positional argument but 2 were given
Request Method: GET
Request URL:    http://127.0.0.1:8000/hello/
Django Version: 3.1.5
Exception Type: TypeError
Exception Value:    
__init__() takes 1 positional argument but 2 were given
Exception Location: C:\Users\usama\OneDrive\Documents\atom\Django\FirstPro\app\views.py, line 7, in index
Python Executable:  C:\Users\usama\anaconda3\envs\MyEnv\python.exe
Python Version: 3.9.1
Python Path:    
['C:\\Users\\usama\\OneDrive\\Documents\\atom\\Django\\FirstPro',
 'C:\\Users\\usama\\anaconda3\\envs\\MyEnv\\python39.zip',
 'C:\\Users\\usama\\anaconda3\\envs\\MyEnv\\DLLs',
 'C:\\Users\\usama\\anaconda3\\envs\\MyEnv\\lib',
 'C:\\Users\\usama\\anaconda3\\envs\\MyEnv',
 'C:\\Users\\usama\\anaconda3\\envs\\MyEnv\\lib\\site-packages']

urls.py

from django.contrib import admin
from django.urls import path
from app import views
urlpatterns = [
    path('hello/', views.index),
    path('admin/', admin.site.urls),
]

views.py

from django.shortcuts import render
from django.http import HttpRequest
# Create your views here.


def index(request):
    return HttpRequest('hello , world!')

installed app in project's setting.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app',
]
FlyingTeller
  • 17,638
  • 3
  • 38
  • 53
  • https://stackoverflow.com/questions/23944657/typeerror-method-takes-1-positional-argument-but-2-were-given – user Jan 27 '21 at 02:07

1 Answers1

2

The reason is you are using HttpRequest but the Django view must return a response. Use this:

from django.http import HttpResponse

def index(request):
    # pay attention this is HttpResponse not HttpRequest
    return HttpResponse('hello , world!')

Mubashar Javed
  • 1,197
  • 1
  • 9
  • 17