为django提供一种基于请求上下文切换模板的机制。
django-variantmpl的Python项目详细描述
我们经常希望为手机、平板电脑和桌面浏览器呈现不同的HTML模板。或用于AB测试。django-variatmpl让它变得简单。通过设置request.variant,可以根据该request.variant呈现模板。这个图书馆深受Action Pack Variants的启发。
快速启动
- 安装django-variantmpl
$ pip install django-variantmpl
- 在视图中将django.shortcuts.render更改为variantmpl.shortcuts.render。
- 并设置request.variant属性。
# views.py --# from django.shortcuts import renderfromvariantmpl.shortcutsimportrender# <- adddefsample(request):# Set variant valuerequest.variant='v2'returnrender(request,'index.html')
- 准备变型模板。
$ echo'sample v1' > templates/index.html $ echo'sample v2' > templates/index+v2.html
- 确认在浏览器中显示views.sample。
- 您可以看到sample v2。
- 这是基于request.variant加载模板(index+v2.html)的结果。
功能
渲染
使用而不是django.shortcuts.render。
# views.py --fromvariantmpl.shortcutsimportrenderdefsample(request):request.variant='v2'# Actually "index+v2.html" is renderedreturnrender(request,'index.html')
呈现回应
使用而不是django.shortcuts.render_to_response。
# views.py --fromvariantmpl.shortcutsimportrender_to_responsedefsample(request):# Actually "index+v2.html" is renderedreturnrender_to_response(request,'index.html',variant='v2')
可以将variant设置为关键字参数。
呈现字符串
使用而不是django.template.loader.render_to_string。
# views.py --fromdjango.httpimportHttpResponsefromvariantmpl.template.loaderimportrender_to_stringdefsample(request):request.variant='v2'# Actually "index+v2.html" is renderedcontent=render_to_string('index.html',request=request)returnHttpResponse(content)
模板响应
使用而不是django.template.response.TemplateResponse。
# views.py --fromdjango.views.genericimportTemplateViewfromvariantmpl.template.responseimportTemplateResponseclassSampleView(TemplateView):template_name='sample/index.html'response_class=TemplateResponse# Replace response classdefget(self,request,**kwargs):request.variant='v2'# Actually "index+v2.html" is renderedreturnsuper().get(request,**kwargs)sample=SampleView.as_view()
猴子修补django的函数/类
很难将所有已经有大代码的代码重写为variantmpl代码。在这种情况下,可以将monkey patch应用于django的函数/类。
警告:此功能是实验性的。如果发生意外的不良影响,以后可能会删除此项。
# settings.py --SECRET_KEY='xxxxxx'# You must write this code below SECRET_KEY.fromvariantmplimportmonkeymonkey.patch_all()
# views.py --# You don't need to replace to 'variantmpl'.fromdjango.shortcutsimportrenderdefsample(request):request.variant='v2'# Actually "index+v2.html" is renderedreturnrender(request,'index.html')
所有猴子修补的目标。
django.shortcuts.render django.shortcuts.render_to_response django.template.loader.render_to_string django.template.response.TemplateResponse.resolve_template They are replaced by the functions/methods of the same name in `variantmpl`.
配置
variantmpl_variant_format
您可以更改variant格式。默认值:+variant。
# settings.py --VARIANTMPL_VARIANT_FORMAT='@{variant}'
# The lookup target template name changes as follows. "index+variant.html" -> "index@variant.html"
variantmpl_property_name
您可以重命名request.variant属性。
# settings.py --VARIANTMPL_PROPERTY_NAME='mutation'
# You can set 'mutation' instead of 'varaiant'request.mutation='v2'
变量模板格式
可以更改插入到模板路径中的变量的位置。
# For example, you have this path.render('sample1/sample2/index.html')# variantmpl inserts the variant(v2) as follows.'sample1/sample2/index+v2.html'# At this time, VARIANTMPL_TEMPLATE_FORMAT is like this. (default)VARIANTMPL_TEMPLATE_FORMAT='{dirpath}{filename}{variant}.{ext}'dirpath# => 'sample1/sample2/'filename# => 'index'variant# => '+v2'ext# => 'html'
像这样更改此格式。
VARIANTMPL_TEMPLATE_FORMAT='{variant}/{dirpath}{filename}.{ext}'# variantmpl inserts the variant(v2) as follows.'+v2/sample1/sample2/index.html'
在这种情况下,模板布局将更改如下
templates ├── +v2 │ └── sample1 │ └── sample2 │ └── index.html └── sample1 └── sample2 └── index.html
python和django支持
- python 3.4以后的版本
- django 1.10之后
- 只支持最新的3个版本。
许可证
麻省理工学院执照。具体条款见许可文件。
历史记录
0.1.0(2017年12月26日)
- 首次发布