Package: django-variantmpl
We often want to render different HTML templates for phones, tablets, and desktop browsers. Or for AB testing.django-variatmplmake it easy. By settingrequest.variant, you can render the template according to thatrequest.variant. This library is heavily inspired byAction Pack Variants.Quick startInstalldjango-variantmpl$pipinstalldjango-variantmplChangedjango.shortcuts.rendertovariantmpl.shortcuts.renderin your views.And setrequest.variantproperty.# views.py --# from django.shortcuts import renderfromvariantmpl.shortcutsimportrender# <- adddefsample(request):# Set variant valuerequest.variant='v2'returnrender(request,'index.html')Prepare variant templates.$echo'sample v1'>templates/index.html$echo'sample v2'>templates/index+v2.htmlConfirmviews.sampledisplay in  your browser.You can seesample v2.It is the result of loading the template(index+v2.html) based onrequest.variant.FeaturesrenderUse instead ofdjango.shortcuts.render.# views.py --fromvariantmpl.shortcutsimportrenderdefsample(request):request.variant='v2'# Actually "index+v2.html" is renderedreturnrender(request,'index.html')render_to_responseUse instead ofdjango.shortcuts.render_to_response.# views.py --fromvariantmpl.shortcutsimportrender_to_responsedefsample(request):# Actually "index+v2.html" is renderedreturnrender_to_response(request,'index.html',variant='v2')You can setvariantas a keyword argument.render_to_stringUse instead ofdjango.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)TemplateResponseUse instead ofdjango.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()Monkey patching Django’s functions/classesIt is difficult to rewrite all code with large codes already tovariantmplcode. In such a case, you can apply Monkey patch to Django’s functions/classes.Caution: This feature is experimental. This may be deleted in the future if unexpected bad effects occur.# 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')All targets for monkey patching.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`.ConfigurationVARIANTMPL_VARIANT_FORMATYou can changevariantformat. default:+variant.# settings.py --VARIANTMPL_VARIANT_FORMAT='@{variant}'# The lookup target template name changes as follows.

"index+variant.html" -> "index@variant.html"VARIANTMPL_PROPERTY_NAMEYou can renamerequest.variantproperty.# settings.py --VARIANTMPL_PROPERTY_NAME='mutation'# You can set 'mutation' instead of 'varaiant'request.mutation='v2'VARIANTMPL_TEMPLATE_FORMATYou can change the position of the variant inserted into template path.# 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'Change this format like this.VARIANTMPL_TEMPLATE_FORMAT='{variant}/{dirpath}{filename}.{ext}'# variantmpl inserts the variant(v2) as follows.'+v2/sample1/sample2/index.html'In this case templates layout will change as followstemplates
  ├── +v2
  │   └── sample1
  │       └── sample2
  │           └── index.html
  └── sample1
      └── sample2
          └── index.htmlPython and Django SupportPython 3.4 laterDjango 1.10 laterSupport only the latest 3 versions.LicenseMIT Licence. See the LICENSE file for specific terms.History0.1.0(12 26, 2017)First release