How to debug this stracktrace in django?

I have a form inside a modal, when I click save I get the following error. The issue is that I dont know where to find it because it’s not showing that the error is in my code.

Environment:


Request Method: POST
Request URL: http://localhost:8000/user/add/

Django Version: 3.1.3
Python Version: 3.9.0
Installed Applications:
['app.apps.AppConfig',
 'users.apps.UsersConfig',
 'crispy_forms',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'debug_toolbar']
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',
 'debug_toolbar.middleware.DebugToolbarMiddleware']



Traceback (most recent call last):
  File "C:UsersbratcaAppDataLocalProgramsPythonPython39libsite-packagesdjangocorehandlersexception.py", line 47, in inner
    response = get_response(request)
  File "C:UsersbratcaAppDataLocalProgramsPythonPython39libsite-packagesdjangocorehandlersbase.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)

Exception Type: TypeError at /user/add/
Exception Value: __init__() takes 1 positional argument but 2 were given

urls – it’s a form view

urlpatterns = [
    path('add/', views.AddUserFormView, name='add-user'),
]

the form view does the post request logic

class AddUserFormView(FormView):
    form_class = TbUserAddForm
    template_name = 'users/add_user_modal.html'
    
    def post(self, request):
        form = self.form_class(request.POST, request.FILES)
        if form.is_valid():
            user_face_img_md5 = Image.open(
                request.FILES['user_face_img_md5'].file)
            user_head_img_md5 = Image.open(
                request.FILES['user_head_img_md5'].file)
            obj = form.save(commit=False)
            response = addUserApi(obj.__dict__,
                                  user_face_img_md5, user_head_img_md5)
            print(f"Request Response [Add User] --> {response.text}")
            return redirect('add-user')

and the form, i think here is the problem with the init. When I instantiate the form, i give the request.POST and request.FILES

class TbUserAddForm(CustomUserCreationForm):

    def __init__(self, user, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.user = user
        if self.user:
            self.fields['role'].queryset = TbRole.objects.all().filter(
                customer=self.user.customer)
            self.fields['department'].queryset = TbDepartment.objects.all().filter(
                customer=self.user.customer)

    class Meta:
        model = TbUser
        fields = [...]


Answer

When you use class-based views in django, you should tell django behave to your class-based view as view in urls.py:

urlpatterns = [
    path('add/', views.AddUserFormView.as_view(), name='add-user'),
]

Be aware that your not passing user to your TbUserAddForm, it would be your next bug probably:

...
form = self.form_class(request.user, request.POST, request.FILES)
...