Django 低级缓存视图

发布于 2024-11-18 01:05:04 字数 2332 浏览 6 评论 0原文

我有一个索引视图,它验证包含各种数据的表单。尽管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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑红尘 2024-11-25 01:05:04

我认为,在这种情况下,您应该使用字符串 ('thankyou' + form.cleaned_data['code']) 作为键。

I think, in this case, you should use string ('thankyou' + form.cleaned_data['code']) as key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文