Skip to content

Instantly share code, notes, and snippets.

@nakagami
Last active May 13, 2021
Embed
What would you like to do?
QR コードを png 画像として返す Django の view関数のコードスニペット
def qrsample(request):
import qrcode # pip install qrcode, pillow
from django.shortcuts import redirect
from django.http.response import HttpResponse
if request.method == "GET":
redirect('/')
qr = qrcode.QRCode(
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=7,
border=5,
)
qr.add_data(request.POST.get('s'))
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
response = HttpResponse(content_type="image/png")
img.save(response, "PNG")
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment