18👍
✅
You can overwrite the FormMixin
‘s get_form_kwargs
method to add the request for to the form’s init parameters:
class ItemCreate(FormView):
def get_form_kwargs(self):
kwargs = super(ItemCreate, self).get_form_kwargs()
kwargs.update({
'request' : self.request
})
return kwargs
0👍
Overriding the form.get_initial() works for me
class ItemCreate(FormView):
def get_initial(self):
init = super(ItemCreate, self).get_initial()
init.update({'request':self.request})
return init
And in the clean we can access it with form.initial dict
class sampleForm(forms.Form):
...
...
def clean(self):
user_request = self.initial['request']
By the way, we don’t need to pop the extra args like suggested above.
- What is difference between instance namespace and application namespace in django urls?
- How to restrict Django Rest Framework browsable API interface to admin users
- Programmatically Upload Files in Django
- Django – Perform Raw SQL Update/Delete and return row count
Source:stackexchange.com