您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關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"> <h2>生成二維碼</h2> <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" ></textarea> </p> </div> <div class="span2 encrypt_type"> <button class="btn btn-primary" onclick="submsg()" >生成二維碼 -></button> </div> <div class="span5"> <label>二維碼:</label> <div > {# 圖片#} </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>
這樣就可以動態生成二維碼了。
感謝各位的閱讀!關于“Python中django框架輸入漢字,數字,字符生成二維碼的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。