Django low level cache views
I have an index view which validates a form containing various data. Even though the thankyou.html page doesn't have complex calculations to kill the server, i would like to render aa slighlty different html page if thankyou.html is already low level cached. To tell you the truth, I don't know what key to pass it... Here is the code.
def index(request):
form = UserForm()
message = 'Incorrect data!'
if request.method == 'POST':
form = UserForm(request.POST)
if form.is_valid():
try:
User.objects.get(code=form.cleaned_data['code'])
except (KeyError, ObjectDoesNotExist):
u = User(lastname=form.cleaned_data['lastname'], surname=form.cleaned_data['surname'], address=form.cleaned_data['address'], email=form.cleaned_data['email'], phone=form.cleaned_data['phone'], code=form.cleaned_data['code'], )
u.save()
return HttpResponseRedirect('/thanks/')
#return redirect('thankyou')
return render_to_response('index.html',{'message': message,'form' : form}, context_instance=RequestContext(request)).
I guess this is the way I should low level cache it:
if form.is_valid():
key = ???
cached_html = cache.get (key)
try:
User.objects.get(code=form.cleaned_data['code'])
except (KeyError, ObjectDoesNotExist):
u = User(lastname=form.cleaned_data['lastname'], surname=form.cleaned_data['surname'], address=form.cleaned_data['address'], email=form.cleaned_data['email'], phone=form.cleaned_data['phone'], code=form.cleaned_data['code'], )
u.save()
if not cached_html:
cached_html = render_to_response('ty.html',{ }, context_instance=RequestContext(request))
cache.set(key, cached_html, time_until_midnight())
return HttpResponseRedirect('/thanks/')
#return redirect('thankyou')
我认为在这种情况下,你应该使用字符串('thankyou'+ form.cleaned_data ['code'])作为键
链接地址: http://www.djcxy.com/p/9672.html上一篇: 在django包含模板标签中处理请求
下一篇: Django低级缓存视图