Получение 'TypeError: объект' NoneType 'не вызывается', когда вызывается self.form_valid (form).
Я получил много решений от stackoverflow для такого рода ошибок, я пробовал все из них, но до сих пор ошибка есть. Прежде чем перейти к ошибке, я хочу описать мой вид, форму и html форму ввода.
view.py
class CancelView(FormView):
template_name = 'cancel.html'
form_class = CancelForm
def post(self, request):
form = self.form_class(request.POST)
if form.is_valid():
if form.cleaned_data.get('canceled'):
try:
print(form)
except Exception:
return self.form_invalid(form)
return self.form_valid(form)
else:
return redirect(reverse_lazy('MyPage'))
return self.form_valid(form)
Здесь form.is_valid () и form.cleaned_data.get ('отменено') работают нормально. Если я распечатаю форму здесь, она возвращает:
<input type="hidden" name="canceled" value="True" id="id_canceled">
form.py
class CancelForm(forms.Form):
use_required_attribute = False
canceled = forms.BooleanField(
initial=False,
widget=forms.HiddenInput(),
required=False,
)
HTML Шаблон
<form action="." method="POST">
<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
<p class="text-center link__btn--block"><input value="Decline Cancellation." class="w50" type="submit"></p>
<input id="id_canceled" name="canceled" type="hidden" value="False">
</form>
Журнал ошибок
Traceback (most recent call last):
File "/Users/mahbubcseju/Desktop/projects/works/bidding-demo/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/mahbubcseju/Desktop/projects/works/bidding-demo/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/mahbubcseju/Desktop/projects/works/bidding-demo/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/mahbubcseju/Desktop/projects/works/bidding-demo/env/lib/python3.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/mahbubcseju/Desktop/projects/works/bidding-demo/env/lib/python3.7/site-packages/django/utils/decorators.py", line 45, in _wrapper
return bound_method(*args, **kwargs)
File "/Users/mahbubcseju/Desktop/projects/works/bidding-demo/env/lib/python3.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/Users/mahbubcseju/Desktop/projects/works/bidding-demo/env/lib/python3.7/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/Users/mahbubcseju/Desktop/projects/works/bidding-demo/env/lib/python3.7/site-packages/django/views/generic/edit.py", line 133, in get
return self.render_to_response(self.get_context_data())
File "/Users/mahbubcseju/Desktop/projects/works/bidding-demo/env/lib/python3.7/site-packages/django/views/generic/edit.py", line 66, in get_context_data
kwargs['form'] = self.get_form()
File "/Users/mahbubcseju/Desktop/projects/works/bidding-demo/env/lib/python3.7/site-packages/django/views/generic/edit.py", line 33, in get_form
return form_class(**self.get_form_kwargs())
TypeError: 'NoneType' object is not callable