Django生成二维码
2024-10-20 11:32:01
1. 安装
pip install qrcode
安装Image包
pip install Image
1.1 在代码中使用
import qrcode img = qrcode.make('输入一个网址')
# img <qrcode.image.pil.PilImage object at 0x1044ed9d0> with open('test.png', 'wb') as f:
img.save(f)
这样会生成一个带有网址的二维码,文件会保存到硬盘中
1.2 在python中调用
import qrcode
from qrcode.image.pure import PymagingImage
img = qrcode.make('Some data here', image_factory=PymagingImage)一个
2. 在django中使用
2.1 新建一个项目,tools应用
django-admin.py startproject xiangmu
python manage.py startapp tools
2.2 将tools应用添加到项目配置文件中
INSTALLED_APPS = ( ... 'tools',
)
2.3 修改tools下的视图
from django.http import HttpResponse
import qrcode
from django.utils.six import BytesIO 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
2.4 添加视图函数在项目/urls中
url(r'^qrcode/(.+)$', 'tools.views.generate_qrcode', name='qrcode'),
2.5 同步数据库,打开开发服务器
python manage.py syncdb
python manage.py runserver
最新文章
- 自定义 Material Design风格的提示框
- (ORA-12899) 10g数据库导入到11g数据库时报错
- httpsClient抓取证书
- spart快速大数据分析学习提纲(一)
- 轻量级的中文分词工具包 - IK Analyzer
- if简单的写法之范围
- python自动发邮件
- 新概念英语(1-119)who call out to the thieves in the dark?
- Net Core Docker 容器部署,修改,保存
- C++11 vector使用emplace_back代替push_back
- JxBrowser之五:清除cache和cookie以及SSL证书处理
- spring-boot-2.0.3应用篇 - shiro集成
- java Concurrent包学习笔记(七):ConcurrentHashMap
- hud 3123 GCC
- 新概念 Lesson 3 Nice to meet you
- Maven解读:强大的依赖体系
- 实现asp.net的文件压缩、解压、下载
- 《DSP using MATLAB》 Problem 2.3
- 峰Spring4学习(7)spring对JDBC的支持
- 优化技术之Android UI优化
热门文章
- sublime text3 注册码
- 键值编码 KVC
- C++11 多线程相关的头文件
- Educational Codeforces Round 18 B
- 线段树(单点更新) HDU 1754 I Hate It
- linux的目录结构详细介绍
- 利用uiautomator实现Android移动app启动时间的测试
- (025)[系统故障]XP下禁止将串口设备识别成鼠标(转)
- 在虚拟机里安装windows或Linux系统时,安装窗口过大按钮有时点不到解决办法(图文详解)
- Design Patterns Uncovered: The Chain Of Responsibility Pattern