用Python来生成二维码,需要qrcode模块,qrcode模块依赖Image 模块,所以首先安装这两个模块
生成二维码保存图片在本地
import qrcode img = qrcode.make('http://www.jxiou.com') # img <qrcode.image.pil.PilImage object at 0x1044ed9d0> with open('test.png', 'wb') as f: img.save(f)
Python中调用:
import qrcode from qrcode.image.pure import PymagingImage img = qrcode.make('Some data here', image_factory=PymagingImage)
Django 中使用
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" type="text/css" href="/static/css/tou.css"> </head> <body> <img src="/bugyanzhm/"/> </body> </html>
路由映射
from django.conf.urls import url from django.contrib import admin from app1 import views urlpatterns = [ url(r'admin/', admin.site.urls), #路由映射admin数据库管理 url(r'articles/', views.special), url(r'yanzhm/', views.yanzhm) ]
逻辑处理
from django.shortcuts import render,redirect,HttpResponse import qrcode from django.utils.six import BytesIO #逻辑处理模块 def special(request): return render(request, 'app1/index.html') def yanzhm(request): img = qrcode.make('http://www.jxiou.com/') #传入网站计算出二维码图片字节数据 buf = BytesIO() #创建一个BytesIO临时保存生成图片数据 img.save(buf) #将图片字节数据放到BytesIO临时保存 image_stream = buf.getvalue() #在BytesIO临时保存拿出数据 response = HttpResponse(image_stream, content_type="image/jpg") #将二维码数据返回到页面 return response