Hi there,
I have a form, which I am initializing its field with some data passing to it from view in its __init__()
method to be shown on front end, using HttpRequest
object. Even though, I have passed the HttpRequest
object named request
in both GET
and POST
methods, I am still getting the following error on POST
request:
TypeError at /accounts/invite-user/cancel/
InviteUserCancelForm.__init__() missing 1 required positional argument: 'request'
Error full details are:
Traceback Switch to copy-and-paste view
D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\core\handlers\exception.py, line 55, in inner
response = get_response(request) …
Local vars
D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\core\handlers\base.py, line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) …
Local vars
D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\views\generic\base.py, line 84, in view
return self.dispatch(request, *args, **kwargs) …
Local vars
D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\contrib\auth\mixins.py, line 73, in dispatch
return super().dispatch(request, *args, **kwargs) …
Local vars
D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\contrib\auth\mixins.py, line 109, in dispatch
return super().dispatch(request, *args, **kwargs) …
Local vars
D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\views\generic\base.py, line 119, in dispatch
return handler(request, *args, **kwargs) …
Local vars
D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\views\generic\edit.py, line 151, in post
form = self.get_form() …
Local vars
D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\views\generic\edit.py, line 39, in get_form
return form_class(**self.get_form_kwargs()) …
My forms.py
is:
class InviteUserCancelForm(forms.Form):
invitations = forms.ChoiceField(
widget=forms.RadioSelect,
choices=(('test_invite','test_invite'),)
)
def __init__(self, request: HttpRequest, *args, **kwargs):
super (InviteUserCancelForm, self).__init__(*args, **kwargs)
if "invitations" in args:
self.fields['invitations'] = forms.ChoiceField(
label="Your Invitations",
widget=forms.RadioSelect,
choices=request["invitations"]
)
else:
self.fields['invitations'] = forms.ChoiceField(
label="Your Invitations",
widget=forms.RadioSelect,
choices=request.session["invitations"]
)
and my views.py
is:
class InviteUserCancel(LoginRequiredMixin, PermissionRequiredMixin, FormView):
permission_required = ("accounts.workspace.can_view", "accounts.workspace.can_change")
permission_denied_message = "ACCESS DENIED: Only Administrators or Workspace \
Administrators can view this page."
template_name = "accounts/invite-user-cancel.html"
extra_context = {"title": "Invitation Sent"}
form_class = InviteUserCancelForm
success_url = "/accounts/invite-user/cancel/done/"
def get(self, request):
invites = Invitation.objects.filter(created_by=request.user)
if request.user.is_superuser:
invites = Invitation.objects.all()
invitations = []
for invite in invites:
invitations.append(tuple((invite.pk, invite.__str__()),))
self.object = tuple(invitations)
request.session["invitations"] = self.object
form = InviteUserCancelForm(request)
return self.render_to_response(self.get_context_data(form=form))
def form_invalid(self, form, request):
messages.error(
request,
"ERROR: You did not fill all the fields of the form \
or you did not fill the form properly."
)
return self.render_to_response(self.get_context_data(form=form))
def form_valid(self, form, request):
try:
invitation = self.model.objects.get(pk=int(form["invitations"]))
invitation.cancel()
return HttpResponseRedirect(self.get_success_url())
except Invitation.DoesNotExist:
form = InviteUserCancelForm(request, request.POST)
return self.form_invalid(form, request)