基于django的生成二维码的接口

原理就是在视图层写一个将数据生成二维码的视图函数:

def generate_qrcode(request, data):
    img = qrcode.make(data)

    buf = BytesIO()
    img.save(buf)
    image_stream = buf.getvalue()

    response = HttpResponse(image_stream, content_type='image/png')
    return response

然后在urls.py中注册路由:

    url(r'^qrcode/(.+)$', generate_qrcode, name='qrcode')

启动服务器就可以使用自己的接口去生成二维码了。

构造函数的时候要导入两个包:

import qrcode
from django.http import HttpResponse
from django.utils.six import BytesIO
原文地址:https://www.cnblogs.com/zzy0306/p/8427299.html