Created
January 21, 2012 04:47
-
-
Save ajamaica/1651327 to your computer and use it in GitHub Desktop.
QR codes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | |
""" | |
Uso | |
{{ my_string|qrcode:"my alt" }} | |
<img src="http://chart.apis.google.com/chart?chs=150x150&cht=qr&chl=my_string&choe=UTF-8" 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"""<img class="qrcode" src="%s" width="150" height="150" alt="%s" />""" % (url, alt)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment