当我在 django 1.4 中打开会话时,一切都很好。如果我使用一些表单流程重定向我的页面,我可以阅读我的会话
例子 :
<form id="processForm" action="http://localhost:8000" method="post">{% csrf_token %}
<input type="submit" value="Search"></input>
</form>
我在 localhost:8000 和 memcached 工作。问题是当我打开会话后,如果我在浏览器上搜索 localhost:8000,我无法读取会话,就像它永远不会退出一样。
代码:
视图.py
def search(request):
c = {}
c.update(csrf(request))
if request.session.get('name'):
c.update({ 'hellomsg' : 'Hello:' + request.session["name"] })
else:
c.update({ 'hellomsg' : 'Hello: Guest' })
if request.method == 'POST':
if request.POST['q']:
if not request.POST['q'] == "false":
c.update({ 'output' : request.POST['q'] })
return render_to_response("form.html", c, context_instance= RequestContext(request))
def open_session(request):
c = {}
c.update(csrf(request))
if not request.session.get('name'):
if request.POST['nametxt']:
request.session['name'] = request.POST['nametxt']
c.update({ 'hellomsg' : 'Hello:' + request.POST['nametxt'] })
else:
c.update({ 'hellomsg' : 'Error...'})
else:
c.update({ 'hellomsg' : 'You're already logged in...'})
return render_to_response("form.html", c, context_instance= RequestContext(request))
def close_session(request):
c = {}
c.update(csrf(request))
if request.session.get('name'):
try:
del request.session['name']
c.update({ 'hellomsg' : 'logged out...'})
except KeyError:
pass
else:
c.update({ 'hellomsg' : 'Error'})
return render_to_response("form.html", c, context_instance= RequestContext(request))
网址.py
urlpatterns = patterns('',
('^$', search),
('^session/$', open_session),
('^csession/$', close_session),
)