我有一个索引视图,用于验证包含各种数据的表单。尽管thankyou.html页面没有复杂的计算来杀死服务器,但是如果thankyou.html已经是低级缓存,我想呈现一个不同的html页面。说实话,我不知道传递它的关键...这是代码。
def index(请求):
form = UserForm()
message ='数据不正确!'
如果request.method =='POST':
form = UserForm(request.POST)
如果form.is_valid():
尝试:
User.objects.get(code = form.cleaned_data ['code'])
除了(KeyError,ObjectDoesNotExist):
u =用户(lastname = form.cleaned_data ['lastname'], 姓= form.cleaned_data [ '姓'], 地址= form.cleaned_data [ '地址'], 电子邮件= form.cleaned_data [ '电子邮件'], 电话= form.cleaned_data [ '电话'], 代码= form.cleaned_data [ '代码'], )
u.save()
返回HttpResponseRedirect('/ thanks /')
#return redirect('thankyou')
返回render_to_response('index.html',{'message':message,'form':form},context_instance = RequestContext(request))。
我想这是我应该低级缓存它的方式:
如果form.is_valid():
key = ???
cached_html = cache.get(key)
尝试:
User.objects.get(code = form.cleaned_data ['code'])
除了(KeyError,ObjectDoesNotExist):
u =用户(lastname = form.cleaned_data ['lastname'],
姓= form.cleaned_data [ '姓'],
地址= form.cleaned_data [ '地址'],
电子邮件= form.cleaned_data [ '电子邮件'],
电话= form.cleaned_data [ '电话'],
代码= form.cleaned_data [ '代码'],
)
u.save()
如果不是cached_html:
cached_html = render_to_response('ty.html',{}, context_instance = RequestContext的(请求))
cache.set(key,cached_html,time_until_midnight())
返回HttpResponseRedirect('/ thanks /')
#return redirect('thankyou')
答案 0 :(得分:3)
我认为在这种情况下,你应该使用string('thankyou'+ form.cleaned_data ['code'])作为键