import urllib from django import template from django.template.defaultfilters import stringfilter from django.utils.html import conditional_escape from django.utils.safestring import mark_safe register = template.Library() @register.filter @stringfilter def qrcode(value, alt=None): """ Generate QR Code image from a string with the Google charts API http://code.google.com/intl/fr-FR/apis/chart/types.html#qrcodes Exemple usage -- {{ my_string|qrcode:"my alt" }} my alt """ url = conditional_escape("http://chart.apis.google.com/chart?%s" % \ urllib.urlencode({'chs':'150x150', 'cht':'qr', 'chl':value, 'choe':'UTF-8'})) alt = conditional_escape(alt or value) return mark_safe(u"""%s""" % (url, alt))