Django中的二维码生成器_html

创建虚拟环境

python3.10 -m venv .

激活环境

source bin/activate

安装 Django、Pillow 和 qrcode

pip install Django Pillow qrcode

启动 Django 项目

django-admin startproject core .

创建应用

python manage.py startapp qrcodeapp

打开 settings.py 文件并将创建的应用程序添加到已安装的应用程序中。

# settings.py

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'qrcodeapp', # add qrcodeapp app
]

创建一个媒体目录以保存所有生成的二维码图像。此媒体目录应在根目录中创建。

现在在 settings.py 文件中指定您的媒体目录,如下所示。

# settings.py

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

现在打开 qrcodeapp 的 views.py 编写生成二维码的逻辑,然后我们在模板上渲染它。

# qrcodeapp/views.py

from django.shortcuts import render
from django.conf import settings
from qrcode import *
import time

def qr_gen(request):
if request.method == 'POST':
data = request.POST['data']
img = make(data)
img_name = 'qr' + str(time.time()) + '.png'
img.save(settings.MEDIA_ROOT + '/' + img_name)
return render(request, 'index.html', {'img_name': img_name})
return render(request, 'index.html')

在根目录中创建一个模板目录,并在 settings.py 中指定路径,如下所示。

# settings.py

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [ BASE_DIR / 'templates' ], # this
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

打开 index.html 并添加以下代码

<!-- templates/index.html -->

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>QR code generator</title>

<style> *{
box-sizing: border-box;
font-family: sans-serif;
}
main{
width: 100%;
max-width: 600px;
margin: 0 auto;
}

input{
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}

button{
width: 100%;
max-width: 200px;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
background-color: #eee;

}

button:hover{
background-color: #ddd;
}

.qr-img{
width: 100%;
max-width: 300px;
margin: 0 auto;
}

.qr-img img{
width: 100%;
}
</style>
</head>
<body>
<main>
<h1>
QR code generator
</h1>
<form method="post">
{% csrf_token %}
<input type="text" name="data" id="data" placeholder="write something or enter url">
<button>Generate</button>
</form>

<div class="qr-img">
{% if img_name %}
<img src="/media/{{ img_name }}" alt="qr code">
{% endif %}
</div>
</main>
</body>
</html>

在 qrcodeapp 目录中新建 urls.py 文件并添加以下代码

# qrcodeapp/urls.py

from django.urls import path
from . import views

urlpatterns = [
path('', views.qr_gen, name='qr_gen'),
]

现在在主项目的 urls.py 中包含这个路径。

# core/urls.py

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('qrcodeapp.urls')),
]

if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

现在一切都完成了,使用以下命令运行服务器。你必须对你的环境没问题。

python manage.py runserver

现在只需在浏览器上打开http://127.0.0.1:8000,输入一些内容并生成您的二维码。
django中的二维码生成器

文中代码项目地址:https://github.com/SoniArpit/simple-qrcode-django-app.git