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 a slightly 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')
I think, in this case, you should use string ('thankyou' + form.cleaned_data['code'])
as key.