1
給定一個Django的形式,我有以下save()
方法:Django中獲取會話數據表單
def save(self):
name = self.cleaned_data.get('name', None)
user = User.objects.get(1) #I want the session user ID from here
所以這樣做,我進口,因爲這樣:
from django.http import HttpRequest as request
,並在上面的代碼中,我寫
request.session.get('user_id') #for example
然而,在運行時,Django的引發以下錯誤:
AttributeError: type object 'HttpRequest' has no attribute 'session'
有沒有更好的方法來做這個檢索?
'def save(self,request)'和'form.save(request)'做的訣竅! – bryansis2010