Given a Django form, I have the following save()
method:
def save(self):
name = self.cleaned_data.get('name', None)
user = User.objects.get(1) #I want the session user ID from here
So to do so I imported as so:
from django.http import HttpRequest as request
and in the above code I wrote
request.session.get('user_id') #for example
However, when run, Django throws the following error:
AttributeError: type object 'HttpRequest' has no attribute 'session'
Is there a better way to do this retrieval?
You can't just import the HttpRequest class and expect it to have the values from the current request. You need to actually pass in that current request from the view.