Home > Mobile >  Django genecric.FormView: 'NoneType' object is not callable
Django genecric.FormView: 'NoneType' object is not callable

Time:10-18

I'm trying to render a really basic FormView but it says it can't get the form model. Full error:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.10/site-packages/django/views/generic/base.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/django/views/generic/base.py", line 142, in dispatch
    return handler(request, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/django/views/generic/edit.py", line 144, in get
    return self.render_to_response(self.get_context_data())
  File "/usr/local/lib/python3.10/site-packages/django/views/generic/edit.py", line 74, in get_context_data
    kwargs["form"] = self.get_form()
  File "/usr/local/lib/python3.10/site-packages/django/views/generic/edit.py", line 39, in get_form
    return form_class(**self.get_form_kwargs())

Exception Type: TypeError at /new-game/
Exception Value: 'NoneType' object is not callable

Form:

class NewGameForm(ModelForm):
    class Meta:
        model = models.Game
        fields = ['title', 'bank_money', 'player_starting_money', 'golden_card_amount']

View:

class NewGameView(generic.FormView):
    template_name = 'games/create_game.html'
    form = forms.NewGameForm

Note that django development server is running on a linux docker container with python 3.10.7

CodePudding user response:

The form class of a FormView is given by the form_class class attribute, not form.

class NewGameView(generic.FormView):
    template_name = 'games/create_game.html'
    form_class = forms.NewGameForm

See Form handling with class-based views

  • Related