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的垃圾回收机制深入分析
Jul 16 Python
朴素贝叶斯算法的python实现方法
Nov 18 Python
举例详解Python中循环语句的嵌套使用
May 14 Python
python3.6 +tkinter GUI编程 实现界面化的文本处理工具(推荐)
Dec 20 Python
tensorflow入门之训练简单的神经网络方法
Feb 26 Python
python学习基础之循环import及import过程
Apr 22 Python
Python中py文件引用另一个py文件变量的方法
Apr 29 Python
PyCharm代码提示忽略大小写设置方法
Oct 28 Python
Python opencv实现人眼/人脸识别以及实时打码处理
Apr 29 Python
opencv之为图像添加边界的方法示例
Dec 26 Python
简单了解Python变量作用域正确使用方法
Jun 12 Python
使用Python爬虫爬取小红书完完整整的全过程
Jan 19 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
一家之言的经验之谈php+mysql扎实个人基本功
2008/03/27 PHP
snoopy 强大的PHP采集类使用实例代码
2010/12/09 PHP
thinkphp3.x自定义Action、Model及View的简单实现方法
2016/05/19 PHP
jQuery find和children方法使用
2011/01/31 Javascript
jQuery Tools tab(幻灯片)
2012/07/14 Javascript
IE网页js语法错误2行字符1、FF中正常的解决方法
2013/09/09 Javascript
Node.js开发指南中的简单实例(mysql版)
2013/09/17 Javascript
使用javascript实现ListBox左右全选,单选,多选,全请
2013/11/07 Javascript
javascript 闭包详解
2015/02/15 Javascript
JS判断是否长按某一键的方法
2016/03/02 Javascript
JS实现把鼠标放到链接上出现滚动文字的方法
2016/04/06 Javascript
AngularJS基础 ng-cut 指令介绍及简单示例
2016/08/01 Javascript
js实现canvas图片与img图片的相互转换的示例
2017/08/31 Javascript
Vue-router结合transition实现app前进后退动画切换效果的实例
2017/10/11 Javascript
Vue使用vux-ui自定义表单验证遇到的问题及解决方法
2018/05/10 Javascript
Vue动态生成表格的行和列
2019/07/18 Javascript
javascript自定义日期比较函数用法示例
2019/07/22 Javascript
vue使用原生swiper代码实例
2020/02/05 Javascript
jQuery实现中奖播报功能(让文本滚动起来) 简单设置数值即可
2020/03/20 jQuery
介绍Python中内置的itertools模块
2015/04/29 Python
KMP算法精解及其Python版的代码示例
2016/06/01 Python
python使用BeautifulSoup与正则表达式爬取时光网不同地区top100电影并对比
2019/04/15 Python
关于Python 的简单栅格图像边界提取方法
2019/07/05 Python
Python坐标线性插值应用实现
2019/11/13 Python
keras 获取某层输出 获取复用层的多次输出实例
2020/05/23 Python
什么是Python中的顺序表
2020/06/02 Python
Python astype(np.float)函数使用方法解析
2020/06/08 Python
python 删除系统中的文件(按时间,大小,扩展名)
2020/11/19 Python
使用纯 CSS 创作一个脉动 loader效果的源码
2018/09/28 HTML / CSS
压铸汽车模型收藏家:Diecastmodelswholesale.com
2016/12/21 全球购物
精油和天然健康美容产品:Art Naturals
2018/01/27 全球购物
祖国在我心中演讲稿500字
2014/05/04 职场文书
写给孩子的新学期寄语
2015/02/27 职场文书
会议主持词结束语
2015/07/03 职场文书
MongoDB安装使用并实现Python操作数据库
2021/06/28 MongoDB
Mysql的Table doesn't exist问题及解决
2022/12/24 MySQL