Reputation: 2244
I am currently using Django and Django sessions to take text a user entered and then use sessions to remember it and use the text in different views. Its sort of like a make-shift search engine, where the text the user enters is saved as a session. However, I would like to have display the last few (say five or so) sessions, so the user can see what their last searches were. Here is my relevant views.py section where the session is stored/ processed:
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)
Upvotes: 0
Views: 40
Reputation: 22697
Instead of having tick, you may have a list of tick. And if you want to get the last tick
, just get the last element of the list.
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)
Then in your template, "search.html"
to display the tickets: use {{tick_list}}
Upvotes: 1