def print_qr_code(request, user_id, product_id):
item = Item.objects.get(pk=signer.unsign(product_id))
qr_filename = str(item.pk) + ".png"
save = False
if item.qr_code is None: #new item!
save = True
url = "/users/" + str(user_id) + "/found/" + str(product_id)
uri = request.build_absolute_uri(url)
item.qr_code = uri
item.save()
url = item.qr_code
qr = QRCode(version=20, error_correction=ERROR_CORRECT_M)
qr.add_data(url)
qr.make()
img = qr.make_image()
if save:
img.save(settings.MEDIA_ROOT + qr_filename) #TODO: In production, we have to check where the QR image is being saved
template_url = settings.MEDIA_URL + qr_filename
return render(request, 'lostnfound/qr_code.html', {'qr_url': template_url , 'item': item})
#A user wants to change an item's settings
评论列表
文章目录