Last active
May 13, 2021 11:43
-
-
Save nakagami/686c116cdba6d246a16413d208d21cb2 to your computer and use it in GitHub Desktop.
QR コードを png 画像として返す Django の view関数のコードスニペット
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
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