并非所有 Django 的功能都适用于 App Engine。因此,由于 App Engine 基础架构的限制,您尝试使用的功能对于 App Engine Django 库是不可接受的。
如果我理解正确,你想缓存整个页面,或者换句话说,整个视图的响应?你可以这样做(只是例子):
# Django on App Engine view example
from google.appengine.api import memcache
from django.http import HttpResponse
def cached_index_page(request):
output_html = memcache.get('index_page') # here we "take" from cache
if output is not None:
pass
else:
output_html = get_page_content()
memcache.add('index_page', output_html, 60) # here we "put" to cache" (timeout also here)
HttpResponse(output_html)
出于您的目的,您可以创建 Django 的中间件,并自动缓存您需要的任何页面。
还要确保您从配置文件中删除了 App Engine 上所有不相关/不可接受的内容。考虑到帮助页面(https://developers.google.com/appengine/articles/django),最小的配置如下:
import os
# 'project' refers to the name of the module created with django-admin.py
ROOT_URLCONF = 'project.urls'
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
# 'django.contrib.sessions.middleware.SessionMiddleware',
# 'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.doc.XViewMiddleware',
'google.appengine.ext.ndb.django_middleware.NdbDjangoMiddleware', # NoSQL db middleware
)
INSTALLED_APPS = (
# 'django.contrib.auth',
'django.contrib.contenttypes',
# 'django.contrib.sessions',
'django.contrib.sites',
)
ROOT_PATH = os.path.dirname(__file__)
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or
# "C:/www/django/templates". Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
ROOT_PATH + '/templates',
)
请记住,App Engine 有自己的本地缓存,例如 Python 运行时环境在单个 Web 服务器上的请求之间缓存导入的模块,您可以告诉 App Engine 缓存 CGI 处理程序脚本本身,以及导入的模块。
有用的链接:
https ://developers.google.com/appengine/articles/django-nonrelhttps://developers.google.com/appengine/docs/python/tools/libraries27