我试图在登录时设置“记住我”,并使用LoginView作为视图。是否可以在自定义AuthenticationForm中设置session.set_expiry(0)?
class CustomAuthenticationForm(AuthenticationForm):
username = forms.CharField();
password = forms.CharField(),
remember_me = forms.BooleanField(widget=forms.CheckboxInput(),
label='Remember me')
def clean_remember_me(self):
if not self.cleaned_data['remember_me']:
set expiry immediately <-- here
SESSION_EXPIRE_AT_BROWSER_CLOSE可能可用,但是在这种情况下可以使用session.get_expiry(0)吗?
不编写自定义视图怎么办?
答案 0 :(得分:0)
我认为您可以像request is passed to the form from view这样来做:
def clean_remember_me(self):
if not self.cleaned_data['remember_me']:
self.request.session.set_expiry(0)
...