我目前正在使用Django和Django会话来获取用户输入的文本,然后使用会话记住它并在不同视图中使用文本。它有点像生成班次搜索引擎,用户输入的文本保存为会话。但是,我想显示最后几个(比如五个左右)会话,因此用户可以看到他们上次搜索的内容。这是我存储/处理会话的相关views.py部分:
result = {}
context = RequestContext(request)
t = request.session.get("tick")
if request.method == 'POST':
search = Search(data=request.POST)
if search.is_valid():
ticker = search.cleaned_data['search']
request.session["tick"] = ticker
else:
print search.errors
else:
search = Search()
return render_to_response('ui/search.html', {"result":result}, context)
答案 0 :(得分:1)
您可以拥有一个tick列表,而不是勾选。如果你想获得最后一个tick
,只需获取列表的最后一个元素。
result = {}
context = RequestContext(request)
t = request.session.get("tick")
if request.method == 'POST':
search = Search(data=request.POST)
tick_list = None
if search.is_valid():
ticker = search.cleaned_data['search']
tick_list =request.session.get('tick_list',[])
tick_list.append(tick)
request.session["tick_list"] = tick_list
else:
print search.errors
else:
search = Search()
//here send the tickest to the template
return render_to_response('ui/search.html', {"result":result,'tick_list':tick_list}, context)
然后在您的模板中"search.html"
显示门票:使用{{tick_list}}