Skip to content

Instantly share code, notes, and snippets.

@nakagami
Last active May 13, 2021 11:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nakagami/686c116cdba6d246a16413d208d21cb2 to your computer and use it in GitHub Desktop.
Save nakagami/686c116cdba6d246a16413d208d21cb2 to your computer and use it in GitHub Desktop.
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