Created
November 22, 2011 13:08
-
-
Save becker990/1385627 to your computer and use it in GitHub Desktop.
Generate QR Code image from a string with the Google charts API
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.utils.html import conditional_escape | |
from django.utils.safestring import mark_safe | |
def qrcode(value, alt=None, size = 250): | |
""" | |
Generate QR Code image from a string with the Google charts API | |
http://code.google.com/intl/fr-FR/apis/chart/types.html#qrcodes | |
example; returns the following: | |
<img src="http://chart.apis.google.com/chart?chs=150x150&cht=qr&chl=my_string&choe=UTF-8" alt="my alt" /> | |
""" | |
size = str(size) | |
if not size.isdigit(): | |
size = 250 | |
url = conditional_escape("http://chart.apis.google.com/chart?%s" % \ | |
urllib.urlencode({'chs':size+'x'+size, 'cht':'qr', 'chl':value, 'choe':'UTF-8'})) | |
alt = conditional_escape(alt or value) | |
return mark_safe(u"""<img class="qrcode" src="%s" width="%s" height="%s" alt="%s" />""" % (url,size,size,alt)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import urllib
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe
def qrcode(value, alt=None, size = 250):
"""
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" }}
"""
size = str(size)
if not size.isdigit():
size = 250