[英]Django Python: Save Image when saving model to Amazon S3
I have my Django app configured to upload media files to AWS S3, this works for my uploads fine, what I am trying to do though is generate and save a QR code using python qrcode, but I can't find a way to do this in the docs.我的 Django 应用程序配置为将媒体文件上传到 AWS S3,这适用于我的上传,但我想做的是使用 python 二维码生成并保存二维码,但我找不到这样做的方法在文档中。 It currently saves to Heroku static, which doesn't work as it's only temporary and I have multiple dynos.
它目前保存到 Heroku static,这不起作用,因为它只是暂时的,而且我有多个测功机。
Here is my model so far:到目前为止,这是我的 model:
class Car(models.Model):
make= models.ForeignKey(Make,on_delete=models.CASCADE)
number = models.IntegerField()
deleted = models.BooleanField(default=False)
qrcode = models.ImageField(upload_to="qrcodes",blank=True,null=True)
def save(self, *args, **kwargs):
import qrcode
qrcode_img = qrcode.make('https://myurl.com/?c=' + str(self.id) + '&make=' + str(self.make.id))
filepath = '/img/tmp/r' + str(self.make.id) + 't' + str(self.id) + '.png'
filename = 'r' + str(self.make.id) + 't' + str(self.id) + '.png'
buffered = BytesIO()
qrcode_img.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode()
self.qrcode = img_str
super(Car, self).save(*args, **kwargs)
At the moment it is also ignoring the filename and filepath, only a string gets saved in my database whereas I want the file to upload to S3 and set the link to the file using the file path.目前它也忽略了文件名和文件路径,只有一个字符串保存在我的数据库中,而我希望文件上传到 S3 并使用文件路径设置文件的链接。
Thank you谢谢
I changed approach and used django-qr-codes which renders the QR code on the fly in the template我改变了方法并使用了 django-qr-codes,它在模板中动态呈现 QR 码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.