Python and Django framework generate two-dimensional code method analysis, pythondjango framework

Source: Internet
Author: User

Python and Django framework generate two-dimensional code method analysis, pythondjango framework

This article describes how to generate a QR code using Python and Django framework. We will share this with you for your reference. The details are as follows:

I. package installation and simple use

1.1 It is very easy to generate a QR code using Python. You can check the qrcode package:

pip install qrcode

Qrcode depends on the Image package:

pip install Image

If this package is difficult to install, you can use a Python-only package to implement this function. See the following.

1.2 after installation, you can use it. This program carries a qr command:

qr 'http://www.ziqiangxuetang.com' > test.png

1.3 let's take a look at how to use it in the code.

import qrcodeimg = qrcode.make('http://www.tuweizhong.com')# img <qrcode.image.pil.PilImage object at 0x1044ed9d0>with open('test.png', 'wb') as f:  img.save(f)

In this way, you can generate a QR code with a URL, but you have to save the file to the hard disk.

[Note]: Use of Python-only packages:

Installation:

pip install git+git://github.com/ojii/pymaging.git#egg=pymagingpip install git+git://github.com/ojii/pymaging-png.git#egg=pymaging-png

The usage is roughly the same, on the command line:

qr --factory=pymaging "Some text" > test.png

Python call:

import qrcodefrom qrcode.image.pure import PymagingImageimg = qrcode.make('Some data here', image_factory=PymagingImage)

2. Use in Django

We can use Django to directly return the generated content to the webpage. the operation procedure is as follows:

2.1 create a zqxtqrcode project, tools application:

django-admin.py startproject zqxtqrcodepython manage.py startapp tools

2.2 Add the tools application to the Project settings. py.

INSTALLED_APPS = (  ...  'tools',)

2.3 modify tools/views. py

from django.http import HttpResponseimport qrcodefrom cStringIO import StringIOdef generate_qrcode(request, data):  img = qrcode.make(data)  buf = StringIO()  img.save(buf)  image_stream = buf.getvalue()  response = HttpResponse(image_stream, content_type="image/png")  response['Last-Modified'] = 'Mon, 27 Apr 2015 02:05:03 GMT'  response['Cache-Control'] = 'max-age=31536000'  return response

After processing the returned results, the browser caches the images and increases the reload speed. For details about Cache-Control and Last-Modified, refer to the HTTP protocol.

2.4 Add the view function to zqxtqrcode/urls. py.

url(r'^qrcode/(.+)$', 'tools.views.generate_qrcode', name='qrcode'),

2.5 synchronize the database and open the Development Server:

python manage.py syncdbpython manage.py runserver

Reference: https://pypi.python.org/pypi/qrcode/

PS: here we recommend a QR code online generation tool for your reference:

Online QR code generation tool (enhanced Edition)
Http://tools.jb51.net/transcoding/jb51qrcode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.