3👍
✅
Views.py
def yourview(request):
yourform = SaleFilterForm(request.POST, user=request.user)
Forms.py
class SaleFilterForm(forms.Form):
...
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super(SaleFilterForm, self).__init__(*args, **kwargs)
Then you can use your user inside of your form as self.user
Hope it helps!
👤Lara
-1👍
This is the best simple code in Django to pass user information form views.py to various files is by using a function
in the file, you want to access the current user info (ex:models.py) :
after all imports
`
user = None
def current_user(request):
global user
user = request.user
`
and use the object variable user in the file
in views.py:
from .models import current_user
in any function(eg-def login(request):)
current_user(request)
It is just based on simple sending variable via the function. Simple logic
Source:stackexchange.com