简体   繁体   English

如何将我的Appengine Django自动加入到我的模板中?

[英]How can I automate my Appengine Django into memcaching my templates?

I have an App Engine application that use Django to serve a website. 我有一个使用Django服务网站的App Engine应用程序。

There are "dynamic" and "static" pages (meaning that they don't often change). 有“动态”和“静态”页面(这意味着它们不会经常更改)。 I want to speed up the loading time of my static pages by memcaching the rendered templates. 我想通过处理呈现的模板来加快静态页面的加载时间。

This is how it looks. 这就是它的样子。

I change this code in my view: 我认为以下代码是这样的:

from django.shortcuts import render_to_response

def myview(request):
    return render_to_response('page.html')

by this one: 通过这个:

from django.shortcuts import render_to_response
from google.appengine.api import memcache

TEMPLATE_CACHE = 3600 * 12

def myview(request):
    t = memcache.get("page.html")
    if t is None:
        t = render_to_response('page.html')
        memcache.set("page.html", t, TEMPLATE_CACHE)
    return t

But since I don't want to implement this behavior in each and every on my "static" view, i'm looking for a nice and django way to do this in my urls.py, like this: 但是由于我不想在“静态”视图的每一个中实现此行为,因此我正在urls.py中寻找一种不错的django方法,例如:

urlpatterns = patterns('',
    (r'^index/$',  cacheView('views.index')),
    (r'^page1/$',  'views.page1'),
    (r'^page2/$',  cacheView('views.page2')),
    (r'^page3/$',  cacheView('views.page3')),
    (r'^page4/$',  'views.page4'),
)

Is it possible? 可能吗?

Do you have such a way to do this? 您有这种方法吗? What could you suggest? 你有什么建议?

Presumably your using django-nonrel. 大概是您使用的django-nonrel。 In which case you can use all of Django's normal caching functionality, including the per-view cache decorator which does exactly what you want. 在这种情况下,您可以使用Django的所有常规缓存功能,包括按需使用的按视图缓存装饰器

(Note, for clarity, it doesn't seem to be templates you want to cache, but the output from the view itself.) (请注意,为清楚起见,它似乎不是要缓存的模板,而是视图本身的输出。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM