Skip to content

Instantly share code, notes, and snippets.

@becker990
Created November 22, 2011 13:08
Show Gist options
  • Save becker990/1385627 to your computer and use it in GitHub Desktop.
Save becker990/1385627 to your computer and use it in GitHub Desktop.
Generate QR Code image from a string with the Google charts API
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&amp;cht=qr&amp;chl=my_string&amp;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))
@hossam905
Copy link

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" }}
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