Home > Blockchain >  Django Error: __init__() takes 1 positional argument but 2 were given
Django Error: __init__() takes 1 positional argument but 2 were given

Time:10-07

I am new to Django and I'm trying to create my first project following a tutorial from Udemy, but I encounter this error.

My project has the following folder structure:

-demo

  • __ init__.py
  • admin.py
  • apps.py
  • models.py
  • tests.py
  • urls.py -views.py

-first

  • __ init__.py
  • asgi.py
  • settings.py
  • urls.py
  • wsgi.py

views.py:

from django.shortcuts import render
from django.http import HttpRequest


def first(request):
    return HttpRequest('1st message from views')

demo/urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.first),
]

first/urls.py:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('demo/', include('demo.urls')),
    path('admin/', admin.site.urls),
]

Both __ init__.py files are empty

error:

TypeError at /demo/
__init__() takes 1 positional argument but 2 were given
Request Method: GET
Request URL:    http://127.0.0.1:8000/demo/
Django Version: 3.2.16
Exception Type: TypeError
Exception Value:    
__init__() takes 1 positional argument but 2 were given
Exception Location: D:\Python\DJANGO\first-project\demo\views.py, line 6, in first
Python Executable:  D:\Python\DJANGO\first-project\venv\Scripts\python.exe
Python Version: 3.7.6
Python Path:    
['D:\\Python\\DJANGO\\first-project',
 'D:\\Python\\DJANGO\\first-project',
 'C:\\Users\\biavu\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip',
 'C:\\Users\\biavu\\AppData\\Local\\Programs\\Python\\Python37\\DLLs',
 'C:\\Users\\biavu\\AppData\\Local\\Programs\\Python\\Python37\\lib',
 'C:\\Users\\biavu\\AppData\\Local\\Programs\\Python\\Python37',
 'D:\\Python\\DJANGO\\first-project\\venv',
 'D:\\Python\\DJANGO\\first-project\\venv\\lib\\site-packages']
Server time:    Thu, 06 Oct 2022 12:34:45  0000

CodePudding user response:

You need to return a HttpResponse, not HttpRequest.

A request is what you get as input.

from django.http import HttpResponse

def first(request):
    return HttpResponse('1st message from views')
  • Related