close

[Solved] Django 2.1.3 Error: __init__() takes 1 positional argument but 2 were given

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Django 2.1.3 Error: init() takes 1 positional argument but 2 were given in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Django 2.1.3 Error: init() takes 1 positional argument but 2 were given Error Occurs?

Today I get the following error Django 2.1.3 Error: init() takes 1 positional argument but 2 were given in python.

How To Solve Django 2.1.3 Error: init() takes 1 positional argument but 2 were given Error ?

  1. How To Solve Django 2.1.3 Error: init() takes 1 positional argument but 2 were given Error ?

    To Solve Django 2.1.3 Error: init() takes 1 positional argument but 2 were given Error You need use as_view() at the end of class based views when declaring in the urls:

  2. Django 2.1.3 Error: init() takes 1 positional argument but 2 were given

    To Solve Django 2.1.3 Error: init() takes 1 positional argument but 2 were given Error You need use as_view() at the end of class based views when declaring in the urls:

Solution 1


You need use as_view() at the end of class based views when declaring in the urls:

path('', views.HomeView.as_view(), name='homepage'),

Also, when using login_required decorator, you need to use it on dispatch method of CBV:

from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator

class HomeView(ListView):

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(HomeView, self).dispatch(*args, **kwargs)

Solution 2

In `urls.py replace by this path-code:

path(' ', views.HomeView.as_view(), name='homepage'),
from django.urls import path
from . import views
 
urlpatterns = [
     path(' ', views.HomeView.as_view(), name='homepage'),
]

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read