我想在我的網站中使用會話。我設置了follwing設置在settings.py文件:Django會話丟失數據(空)
'django.contrib.sessions', #in the installed apps
和
SESSION_ENGINE = 'django.contrib.sessions.backends.signed_cookies'
和
'django.contrib.sessions.middleware.SessionMiddleware', # in the midlleware classes
視圖代碼
def apartamente(request):
form = SearchFormApartamente()
execute_form_process = 0
if request.method == 'POST':
form = SearchFormApartamente(request.POST)
execute_form_process = 1
request.session['apartamente'] = form
request.session['vasea'] = True
request.session.set_expiry(300)
request.session.modified = True
elif 'apartamente' in request.session:
form = searchFormApartamente(request.session['apartamente'])
execute_form_process = 1
# forms.py
class SearchFormApartamente(forms.Form):
thewidget = ColumnCheckboxSelectMultiple()
category = forms.ModelMultipleChoiceField(
queryset=Categorie_Oferta.objects.filter(id__in=[1,2,3,4]),
required=False)
sector = forms.ModelMultipleChoiceField(queryset=Sector.objects.all(),
widget=forms.widgets.CheckboxSelectMultiple, required=False)
min_price = forms.IntegerField(required=False,
widget=forms.widgets.Input(attrs={'class': 'rounded'}))
max_price = forms.DecimalField(required=False)
,但問題是,我嘗試在會話中以及何時存儲表單我第二次進入視圖並嘗試從sessiom讀取值,會話爲空... 可能是什麼問題? 在此先感謝!
能否請您發表您的看法碼? – Brandon 2013-03-14 21:27:52
爲了獲得會話,您必須將'get'放在'request.session.get('apartamente')'不是'request.session ['apartamente']'因爲這種格式用於存儲。總體來說@Brandon的回答是正確的。 – catherine 2013-03-15 11:10:30