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

 更新时间:2019年09月24日 10:46:43   作者:张丶耀庆  
这篇文章主要介绍了Python django框架输入汉字,数字,字符转成二维码实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了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 scikit-learn数据预处理常见方法和步骤

    Python scikit-learn数据预处理常见方法和步骤

    数据预处理是数据准备阶段的一个重要环节,主要目的是将原始数据转换成适合机器学习模型使用的格式,数据预处理可以显著提高机器学习模型的性能和准确度,本文给大家介绍了Python数据预处理常见方法和步骤,需要的朋友可以参考下
    2024-05-05
  • Python实现二叉树的常见遍历操作总结【7种方法】

    Python实现二叉树的常见遍历操作总结【7种方法】

    这篇文章主要介绍了Python实现二叉树的常见遍历操作,结合实例形式总结分析了二叉树的前序、中序、后序、层次遍历中的迭代与递归等7种操作方法,需要的朋友可以参考下
    2019-03-03
  • python3使用腾讯企业邮箱发送邮件的实例

    python3使用腾讯企业邮箱发送邮件的实例

    今天小编就为大家分享一篇python3使用腾讯企业邮箱发送邮件的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-06-06
  • 聊聊pytorch测试的时候为何要加上model.eval()

    聊聊pytorch测试的时候为何要加上model.eval()

    这篇文章主要介绍了聊聊pytorch测试的时候为何要加上model.eval()的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-05-05
  • python过滤字符串中不属于指定集合中字符的类实例

    python过滤字符串中不属于指定集合中字符的类实例

    这篇文章主要介绍了python过滤字符串中不属于指定集合中字符的类,涉及Python针对字符串与集合的相关操作技巧,需要的朋友可以参考下
    2015-06-06
  • pytorch实现从本地加载 .pth 格式模型

    pytorch实现从本地加载 .pth 格式模型

    今天小编就为大家分享一篇pytorch实现从本地加载 .pth 格式模型,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • Python实现多张图片合成一张马赛克图片

    Python实现多张图片合成一张马赛克图片

    这篇文章主要介绍了了Python如何实现将多张图片合成一张马赛克图片。文中的示例代码讲解详细,对我们学习Python有一定的帮助,感兴趣的可以学习一下
    2021-12-12
  • import的本质解析

    import的本质解析

    import是我们经常使用的功能,方法大家也都会。虽然用的多,但它的本质我们似乎没有好好的了解过。本文就import的本质解析,向大家作了介绍,需要的朋友可以参考下。
    2017-10-10
  • Python异常之常见的Bug类型解决方法

    Python异常之常见的Bug类型解决方法

    这篇文章主要介绍了Python异常之常见的Bug类型解决方法,主要分享一些粗心导致和知识不熟练导致的语法错误以及被迫掉坑等内容,文章介绍非常详细需要的小伙伴可以参考一下
    2022-03-03
  • python爬取晋江文学城小说评论(情绪分析)

    python爬取晋江文学城小说评论(情绪分析)

    这篇文章主要介绍了使用python爬取晋江文学城小说评论(情绪分析),全文代码详细,逻辑清晰,很适合学习爬虫爬取的朋友,需要的朋友可以参考下
    2021-04-04

最新评论