Skip to main content

Provide a mechanism for Django that switching template based on request context.

Project description

We often want to render different HTML templates for phones, tablets, and desktop browsers. Or for AB testing. django-variatmpl make it easy. By setting request.variant, you can render the template according to that request.variant. This library is heavily inspired by Action Pack Variants.

travis latest version license

Quick start

  1. Install django-variantmpl
$ pip install django-variantmpl
  1. Change django.shortcuts.render to variantmpl.shortcuts.render in your views.
  • And set request.variant property.
# --

# from django.shortcuts import render
from variantmpl.shortcuts import render # <- add

def sample(request):

    # Set variant value
    request.variant = 'v2'

    return render(request, 'index.html')
  1. Prepare variant templates.
$ echo 'sample v1' > templates/index.html
$ echo 'sample v2' > templates/index+v2.html
  1. Confirm views.sample display in your browser.
  • You can see sample v2.
  • It is the result of loading the template(index+v2.html) based on request.variant.



Use instead of django.shortcuts.render.

# --

from variantmpl.shortcuts import render

def sample(request):
    request.variant = 'v2'

    # Actually "index+v2.html" is rendered
    return render(request, 'index.html')


Use instead of django.shortcuts.render_to_response.

# --

from variantmpl.shortcuts import render_to_response

def sample(request):

    # Actually "index+v2.html" is rendered
    return render_to_response(request, 'index.html', variant='v2')

You can set variant as a keyword argument.


Use instead of django.template.loader.render_to_string.

# --

from django.http import HttpResponse

from variantmpl.template.loader import render_to_string

def sample(request):
    request.variant = 'v2'

    # Actually "index+v2.html" is rendered
    content = render_to_string('index.html', request=request)
    return HttpResponse(content)


Use instead of django.template.response.TemplateResponse.

# --

from django.views.generic import TemplateView
from variantmpl.template.response import TemplateResponse

class SampleView(TemplateView):
    template_name = 'sample/index.html'
    response_class = TemplateResponse # Replace response class

    def get(self, request, **kwargs):
        request.variant = 'v2'

        # Actually "index+v2.html" is rendered
        return super().get(request, **kwargs)

sample = SampleView.as_view()

Monkey patching Django’s functions/classes

It is difficult to rewrite all code with large codes already to variantmpl code. 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.

# --

SECRET_KEY = 'xxxxxx'

# You must write this code below SECRET_KEY.
from variantmpl import monkey
# --

# You don't need to replace to 'variantmpl'.
from django.shortcuts import render

def sample(request):
    request.variant = 'v2'

    # Actually "index+v2.html" is rendered
    return render(request, 'index.html')

All targets for monkey patching.


They are replaced by the functions/methods of the same name in `variantmpl`.



You can change variant format. default: +variant.

# --
# The lookup target template name changes as follows.

"index+variant.html" -> "index@variant.html"


You can rename request.variant property.

# --
# You can set 'mutation' instead of 'varaiant'
request.mutation = 'v2'


You can change the position of the variant inserted into template path.

# For example, you have this path.

# variantmpl inserts the variant(v2) as follows.

# 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.

In this case templates layout will change as follows

  ├── +v2
  │   └── sample1
  │       └── sample2
  │           └── index.html
  └── sample1
      └── sample2
          └── index.html

Python and Django Support

  • Python 3.4 later
  • Django 1.10 later
  • Support only the latest 3 versions.


MIT Licence. See the LICENSE file for specific terms.


0.1.0(12 26, 2017)

  • First release

Project details

Release history Release notifications

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
django_variantmpl-0.1.0-py3-none-any.whl (9.5 kB) Copy SHA256 hash SHA256 Wheel py3
django-variantmpl-0.1.0.tar.gz (9.0 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page