用于python应用程序的rendertron中间件
rendertron的Python项目详细描述
python rendertron
Rendertron middleware for Python applications.
Rendertron是无头铬合金 渲染解决方案旨在动态渲染和序列化网页。这个 这个包的目标是提供中间件来使用 Rendertron服务并使结果可用。
这使得可以呈现渐进式web应用程序(pwa),等等 使它完全呈现(完成初始数据加载等)并使用 完全构建标记作为响应。
另外,用户可以更快地看到完全呈现的应用程序 还允许搜索引擎正确索引标记。
安装
按照中的步骤安装rendertron服务 the documentation。
使用pip
:
pip install rendertron
您还可以使用pip
的-e
标志安装最新的开发版本:
pip install -e git://git@github.com:frontendr/python-rendertron.git@develop#egg=rendertron
这将安装develop
分支。
django
首先,将'rendertron'
添加到设置中的INSTALLED_APPS
列表中。
那么您有两个选择:
- 启用middleware并呈现与
RENDERTRON_INCLUDE_PATTERNS
或与中的任何内容都不匹配RENDERTRON_EXCLUDE_PATTERNS
。有关详细信息,请参阅配置部分 那些。 - 使用
@rendertron_render
装饰器装饰特定视图,使其仅允许呈现 使用rendertron服务的那些视图。
中间件
- 将
'rendertron.middleware.DjangoRendertronMiddleware'
添加到MIDDLEWARE
在设置中列出。 - 确保指定
RENDERTRON_INCLUDE_PATTERNS
以指定路径模式 将由rendertron服务或RENDERTRON_EXCLUDE_PATTERNS_EXTRA
提供的 只指定要排除的内容。
装饰特定视图
不依赖于中间件和设置,还可以修饰
使用@rendertron_render
装饰器的特定视图。
fromrendertron.decorators.djangoimportrendertron_render@rendertron_renderdefmy_view(request):...
对于基于类的视图,使用django的^{
fromdjango.utils.decoratorsimportmethod_decoratorfromdjango.views.genericimportTemplateViewfromrendertron.decorators.djangoimportrendertron_render@method_decorator(rendertron_render,name="dispatch")classMyView(TemplateView):...
配置
大多数配置是通过指定特定变量来完成的。对于django用户 这是在你的网站设置中完成的。
以下设置可用:
Setting | Default | Description |
---|---|---|
^{ | ^{ | The url the Rendertron service is listening on. |
^{ | ^{ | The query parameter added to the request url passed to Rendertron. This is used to differentiate normal requests with requests from Rendertron. |
^{ | See Storage | An object literal specifying and configuring the storage class to be used. See the Storage section for more information. |
^{ | ^{ | A list of regular expression patterns to include. Once a pattern in this list matches the request no further checking will be done. |
^{ | List of common extensions. | By default this is a list of common static file type extensions used on the web. If Django is detected it's ^{ |
^{ | ^{ | Like ^{ |
储存
存储类正在处理存储来自 Rendertron服务一段时间。他们处理如果,如何,在哪里,如何 很长一个结果被存储。有一些核心存储类可用 系统的建立是为了让你很容易建立自己的。
选择一个内置存储类取决于您的框架。
任何框架:DummyStorage
一个不做任何事情的存储类。它不会储存也永远不会回来 存储的结果。
要使用它,只需将RENDERTRON_STORAGE['CLASS']
设置为
'rendertron.storage.DummyStorage'
。它没有选择。
django:DjangoCacheStorage
利用django的缓存框架来存储结果的存储类。
要使用它,只需将RENDERTRON_STORAGE['CLASS']
设置为
'rendertron.storage.DjangoCacheStorage'
。它有以下选项:
Setting | Default | Description |
---|---|---|
^{ | Django's ^{ | The number of seconds the result should be stored in the cache. It's the ^{ |
^{ | ^{ | The ^{ |
示例配置:
RENDERTRON_STORAGE={'CLASS':'rendertron.storage.DjangoCacheStorage','OPTIONS':{'TIMEOUT':300,}}
运行测试
首先安装django以测试django相关的东西。
pip install django
然后使用提供的最小设置文件通过django-admin
运行测试。
django-admin test --pythonpath . --settings tests.django.settings
许可证
麻省理工学院