Django 低级缓存视图
我有一个索引视图,它验证包含各种数据的表单。尽管thankyou.html页面没有复杂的计算来杀死服务器,但如果thankyou.html已经是低级缓存,我想渲染一个稍微不同的html页面。说实话,我不知道要传递什么密钥...
这是代码:
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))
我想这是我应该低级缓存它的方式:
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 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')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为,在这种情况下,您应该使用字符串
('thankyou' + form.cleaned_data['code'])
作为键。I think, in this case, you should use string
('thankyou' + form.cleaned_data['code'])
as key.