Python django框架输入汉字,数字,字符生成二维码实现详解


Posted in Python onSeptember 24, 2019

这篇文章主要介绍了Python django框架输入汉字,数字,字符转成二维码实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

模块必备:Python环境 + pillow + qrcode 模块

核心代码<br>import qrcode
 
qr = qrcode.QRCode(
      version=2,
      error_correction=qrcode.constants.ERROR_CORRECT_L,
      box_size=20,
      border=4,
    )
qr.add_data('你要生成的文件')
qr.make(fit=True)
img = qr.make_image()
# 只需要改成自己的路径
img.save('text.png')<br># img.save('/Users/admin/PycharmProjects/str_code/statics/assets/png/'+'text.png')

django views函数代码!路由自己设置就可以。

from django.shortcuts import render
 
# Create your views here.
 
 
import qrcode
# python-qrcode是个用来生成二维码图片的第三方模块,依赖于 PIL 模块和 qrcode 库。
 
 
def str_decode_code(request):
  print(request.method)
  if request.method == 'GET':
    return render(request,'index.html')
  if request.method== 'POST':
    text = request.POST.get('message')
    print(text)
 
    qr = qrcode.QRCode(
      version=2,
      error_correction=qrcode.constants.ERROR_CORRECT_L,
      box_size=20,
      border=4,
    )
    qr.add_data(text)
    qr.make(fit=True)
    img = qr.make_image()
    # 只需要改成自己的路径
    img.save('/Users/admin/PycharmProjects/str_code/statics/assets/png/'+'text.png')
    return render(request,'en_index.html',{'mgs':text}) 

前段代码

<!DOCTYPE html>
<html lang="en">
   
  <head>
    <meta charset="utf-8">
    <title>二维码生成器</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="keywords" content="二维码生成器,二维码扫描,二维码制作,二维码解码,微信二维码,二维码名片,QR code,二维码是什么,微信二维码">
    <meta name="description" content="二维码生成器是国内免费二维码在线服务网站,功能简单、方便、快捷。织梦二维码解决方案应用于各类网站,无论是商业应用还是个人创业都是首选。">
    <link href="../statics/assets/css/bootstrap.css" rel="external nofollow" rel="stylesheet">
    <link href="../statics/assets/css/bootstrap-colorpicker.min.css" rel="external nofollow" rel="stylesheet">
    <style type="text/css">body {
        padding-top: 60px;
        padding-bottom: 40px;
       }
#flink li a {
  color:#999;
}
    </style>
    <link href="../statics/assets/css/bootstrap-responsive.css" rel="external nofollow" rel="stylesheet">
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="../statics/assets/ico/apple-touch-icon-144-precomposed.png.html" rel="external nofollow" >
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="../statics/assets/ico/apple-touch-icon-114-precomposed.png.html" rel="external nofollow" >
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="../statics/assets/ico/apple-touch-icon-72-precomposed.png.html" rel="external nofollow" >
    <link rel="apple-touch-icon-precomposed" href="../statics/assets/ico/apple-touch-icon-57-precomposed.png.html" rel="external nofollow" >
    <link rel="shortcut icon" href="../statics/assets/ico/favicon.png.html" rel="external nofollow" >
  </head>
   
  <body>
    <div class="navbar navbar-inverse navbar-fixed-top">
      <div class="navbar-inner">
        <div class="container">
          <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"> <span class="icon-bar"></span>
 <span class="icon-bar"></span>
 <span class="icon-bar"></span>
          </button> <a class="brand" href="index.html" rel="external nofollow" >二维码生成器</a>
        </div>
      </div>
    </div>
    <div class="container">
      <div class="container">
        <header class="jumbotron subhead" id="overview">
           <h1>生成二维码</h1>
          <p class="lead">用于制作生成二维码,方便各类客户端(例如:微信、淘宝、移动浏览器)进行扫描。</p>
        </header>
        <form action="/code/code" method="post">
        <ul id="myTab" class="nav nav-tabs">
          <li class="active"><a href="#" rel="external nofollow" >文本</a>
          </li>
 
        </ul>
        <div class="row">
          <div class="span5">
            <label>明文:</label>
              <p>
                <textarea name="message" class="span5" style="height: 500px"></textarea>
              </p>
          </div>
          <div class="span2 encrypt_type">
 
            <button style="margin-top:250px" class="btn btn-primary" onclick="submsg()" >生成二维码 -></button>
          </div>
          <div class="span5">
            <label>二维码:</label>
            <div style="height: 500px;border:1px solid #000">
{#              图片#}
 
            </div>
          </div>
        </div>
        </form>
      </div>
 
      <hr>
      <footer>
{#        <p>CopyRight 2015 <a href="" target=" rel="external nofollow" _blank"></a><strong></strong></p>#}
      </footer>
    </div>
    <script src="../statics/assets/js/jquery-1.11.2.min.js"></script>
    <script src="../statics/assets/js/bootstrap.min.js"></script>
    <script src="../statics/assets/js/bootstrap-colorpicker.js"></script>
{#    <script>#}
{#      function submsg(){#}
{##}
{#      }#}
{#    </script>#}
 
  </body>
 
</html>

这样就可以动态生成二维码了。

做好的二维码,访问地址:http://qrcode.ipgou.net/

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中random模块生成随机数详解
Mar 10 Python
python下读取公私钥做加解密实例详解
Mar 29 Python
Scrapy爬虫实例讲解_校花网
Oct 23 Python
python pandas中对Series数据进行轴向连接的实例
Jun 08 Python
使用TensorFlow实现SVM
Sep 06 Python
python 列表中[ ]中冒号‘:’的作用
Apr 30 Python
pyqt 实现为长内容添加滑轮 scrollArea
Jun 19 Python
Django框架之DRF 基于mixins来封装的视图详解
Jul 23 Python
Python线程协作threading.Condition实现过程解析
Mar 12 Python
使用python实现时间序列白噪声检验方式
Jun 03 Python
sublime3之内网安装python插件Anaconda的流程
Nov 10 Python
Python制作简单的剪刀石头布游戏
Dec 10 Python
分享一个pycharm专业版安装的永久使用方法
Sep 24 #Python
python实现的config文件读写功能示例
Sep 24 #Python
python使用socket实现的传输demo示例【基于TCP协议】
Sep 24 #Python
pymysql 开启调试模式的实现
Sep 24 #Python
django2.2安装错误最全的解决方案(小结)
Sep 24 #Python
python爬虫中多线程的使用详解
Sep 23 #Python
Django中自定义模型管理器(Manager)及方法
Sep 23 #Python
You might like
YB217、YB235、YB400浅听
2021/03/02 无线电
用PHP动态生成虚拟现实VRML网页
2006/10/09 PHP
自动分页的不完整解决方案
2007/01/12 PHP
在PHP中养成7个面向对象的好习惯
2010/07/17 PHP
web server使用php生成web页面的三种方法总结
2013/10/28 PHP
php数组使用规则分析
2015/02/27 PHP
PHP正则表达式过滤html标签属性(DEMO)
2016/05/04 PHP
PHPExcel简单读取excel文件示例
2016/05/26 PHP
PHP实现文字写入图片功能
2019/02/18 PHP
你所要知道JS(DHTML)中的一些技巧
2007/01/09 Javascript
Javascript 跨域访问解决方案
2009/02/14 Javascript
js定时器的使用(实例讲解)
2014/01/06 Javascript
关于javascript模块加载技术的一些思考
2014/11/28 Javascript
深入理解Angularjs向指令传递数据双向绑定机制
2016/12/31 Javascript
JavaScript实现旋转轮播图
2020/08/18 Javascript
使用JS实现气泡跟随鼠标移动的动画效果
2017/09/16 Javascript
axios全局注册,设置token,以及全局设置url请求网段的方法
2018/09/25 Javascript
Node 搭建一个静态资源服务器的实现
2019/05/20 Javascript
浅谈微信小程序列表埋点曝光指南
2019/10/15 Javascript
[45:18]2018DOTA2亚洲邀请赛 4.3 突围赛 Optic vs iG 第一场
2018/04/04 DOTA
Python用Bottle轻量级框架进行Web开发
2016/06/08 Python
Windows和Linux下Python输出彩色文字的方法教程
2017/05/02 Python
Python动刷新抢12306火车票的代码(附源码)
2018/01/24 Python
python实现朴素贝叶斯分类器
2018/03/28 Python
对python中使用requests模块参数编码的不同处理方法
2018/05/18 Python
Python字典的基本用法实例分析【创建、增加、获取、修改、删除】
2019/03/05 Python
python 列表中[ ]中冒号‘:’的作用
2019/04/30 Python
python 5个实用的技巧
2020/09/27 Python
回馈慈善的设计师太阳镜:DIFF eyewear
2019/10/17 全球购物
汽车技术服务与营销专业在籍生自荐信
2013/09/28 职场文书
上课睡觉检讨书300字
2014/11/18 职场文书
2019奶茶店创业计划书范本!
2019/07/15 职场文书
导游词之镇江-金山寺
2019/10/14 职场文书
七年级作文之英语老师
2019/10/28 职场文书
Java 超详细讲解十大排序算法面试无忧
2022/04/08 Java/Android
postgresql中如何执行sql文件
2023/05/08 PostgreSQL