为django提供一种基于请求上下文切换模板的机制。

django-variantmpl的Python项目详细描述


我们经常希望为手机、平板电脑和桌面浏览器呈现不同的HTML模板。或用于AB测试。django-variatmpl让它变得简单。通过设置request.variant,可以根据该request.variant呈现模板。这个图书馆深受Action Pack Variants的启发。

traviscoveralls.iolatest versionlicense

快速启动

  1. 安装django-variantmpl
$ pip install django-variantmpl
  1. 在视图中将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')
  1. 准备变型模板。
$ echo'sample v1' > templates/index.html
$ echo'sample v2' > templates/index+v2.html
  1. 确认在浏览器中显示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日)

  • 首次发布

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
由于java的原因,maven无法运行代码。lang.NoClassDefFoundError:com/fasterxml/jackson/annotation/JsonMerge   Android项目中的java Creative SDK图像编辑器UI   java如何在Android Studio中使用DataOutputStream上传文件并将其他参数传递到web服务器   java倒计时服务打开时崩溃   java将RubyonRails项目转换为JRubyonRails项目   java我的图库意图是不显示图像?为什么?   java如何在春季启动时跳过mongodb/   java@Autowired在Spring中是如何实现的   甲骨文Akka java。util。同时发生的timeoutexception线程池频繁超时   java maven依赖项对spring启动应用程序有何影响?   java Firestore执行复合查询,未截获事件“已修改”   java ItemStreamException:未能初始化读取器,原因是:IllegalStateException:流已初始化。重新开放前关闭   java将空标记解组到集合的新实例中   使用AspectJ的java新手:无法调用aspect   java查找棋类游戏的所有组合   你为什么要这样做and==与Java中的equals方法不一样吗?   如何对使用JavaUUID的代码进行单元测试?