Curious about get_form_kwargs in FormView

后端 未结 1 981
逝去的感伤
逝去的感伤 2020-12-09 03:54

I was having trouble with FormView recently and found that the way to go about doing it was to use get_form_kwargs.

Here is my code:

class InternalRe         


        
相关标签:
1条回答
  • 2020-12-09 04:35

    The get_form_kwargs method will return a dictionary with the kwargs that will be passed to the __init__ of your form. Now, if you have a form that expects a kwarg named user and pass it a kwarg named request it will complain with the error you see. If you want to pass request instead of user (this is what I usually do since the request contains the user) then you should define your form class like this:

    class RequestForm(forms.Form):
        def __init__(self, *args, **kwargs):
            self.request = kwargs.pop('request', None)
            super(RequestForm, self).__init__(*args, **kwargs)
    
    0 讨论(0)
提交回复
热议问题