QRCode template tag

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from base64 import b64encode
from django.template.base import Library
import cStringIO
import qrcode


register = Library()


@register.simple_tag
def qrcode_datauri(data, pixel_size=5, border_pixels=1, error_correction="H"):
    qrcode_object = qrcode.QRCode(
        error_correction=getattr(
            qrcode.constants,
            "ERROR_CORRECT_%s" % error_correction,
            "H" 
        ),  
        box_size=max(1, min(100, pixel_size)),
        border=max(1, min(100, border_pixels)),
    )   
    qrcode_object.add_data(data)
    qrcode_object.make(fit=True)
    qrcode_image = qrcode_object.make_image()
    byte_stream = cStringIO.StringIO()
    qrcode_image.save(byte_stream)
    datauri = "data:image/png;base64,%s" % b64encode(byte_stream.getvalue())
    byte_stream.close()
    return datauri

More like this

  1. Handling choices the right way by mallipeddi 6 years, 5 months ago
  2. Update Related Object Fields by johnboxall 5 years, 4 months ago
  3. localsettings by elpaso66 4 years, 1 month ago
  4. Capture template output as a variable by kcarnold 6 years, 3 months ago
  5. Django code generator by jansta 3 years, 7 months ago

Comments

(Forgotten your password?)