我目前正在尝试为我的Django模型中的员工档案生成QR码。我正在关注此网站的教程:https://gilang.chandrasa.com/generate-qr-code-in-django-model/ 但不幸的是,当我创建个人资料时,没有生成QR码。
注意:我正在使用qr-code 5.3而不是5.1,如教程所示。
models.py
from django.db import models
from django.conf import settings
from django.core.urlresolvers import reverse
from django.core.files.uploadedfile import InMemoryUploadedFile
import random, qrcode
from io import StringIO
def upload_location(instance, filename):
return '%s/profile_images/%s/' % (instance.user.username, filename)
def qrcode_location(instance, filename):
return '%s/qr_codes/%s/' % (instance.user.username, filename)
# Create your models here.
class EmployeeProfile(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
image = models.ImageField(upload_to=upload_location,
null=True,
blank=True,
width_field='width',
height_field='height'
)
qrcode = models.ImageField(upload_to=qrcode_location, null=True, blank=True)
height = models.IntegerField(default=0)
width = models.IntegerField(default=0)
first_name = models.CharField(max_length=30)
middle_name = models.CharField(max_length=30, null=True, blank=True)
last_name = models.CharField(max_length=30)
phone_number = models.CharField(max_length=12)
email = models.CharField(max_length=254, unique=True)
address_line1 = models.CharField(max_length=60)
address_line2 = models.CharField(max_length=20, null=True, blank=True)
city = models.CharField(max_length=60)
state = models.CharField(max_length=2)
employee_id = models.CharField(unique=True, max_length=11)
hire_date = models.CharField(max_length=19, null=True, blank=True)
last_screen = models.CharField(max_length=19, null=True, blank=True)
active = models.CharField(max_length=3, default='yes')
def __str__(self):
return self.first_name
"""
Check to see if an image is provided, if not then use a default profile pic
"""
def image_url(self):
if self.image:
return self.image.url
else:
return '/static/images/default-profile-pic.jpg'
def save(self):
first_initial = self.first_name[0].upper()
second_initial = self.last_name[0].upper()
id_number = first_initial + second_initial + str(random.randint(1000000, 9999999))
if not EmployeeProfile.objects.filter(employee_id=id_number).exists():
self.employee_id = id_number
super(EmployeeProfile, self).save()
def generate_qrcode(self):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(reverse('ppm:view-profile', kwargs={'pk': self.id}))
qr.make(fit=True)
img = qr.make_image()
buffer = StringIO()
img.save(buffer)
filename = 'qrcode-%s.png' & self.id
filebuffer = InMemoryUploadedFile(
buffer, None, filename, 'image/png', buffer.len, None
)
self.qrcode.save(filename, filebuffer)
任何想法我可能做错了什么?
--------------------- SOLUTION UPDATE --------------------- ----
在玩了一些代码后,我终于想出了如何生成qr代码:
def save(self):
first_initial = self.first_name[0].upper()
second_initial = self.last_name[0].upper()
id_number = first_initial + second_initial + str(random.randint(1000000, 9999999))
self.generate_qrcode()
if not EmployeeProfile.objects.filter(employee_id=id_number).exists():
self.employee_id = id_number
super(EmployeeProfile, self).save()
def generate_qrcode(self):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data('Some data')
qr.make(fit=True)
filename = 'qrcode-code1.png'
img = qr.make_image()
img.save('media_cdn/' + qrcode_location(self, filename))