[英]how to create a qr-code an sone of one of the fields based on the other when creating an object?
I want to creato objects through admin-pannel Django, I enter a value for a parameter and I want a qr code to be generated based on this value, my code:我想通过管理面板 Django 创建对象,我输入一个参数值,我想根据这个值生成一个二维码,我的代码:
class People(models.Model):
name = models.CharField(max_length=500, unique=True)
qr_code = models.ImageField(upload_to="img/qr_codes/", verbose_name="QR-code", null = True)
def save(self, *args, **kwargs):
qr = qrcode.QRCode(version=2, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=1)
qr.add_data(self.name)
qr.make(fit=True)
qr.make_image().save(f'img/qr_codes/{self.name}.png')
self.qr_code = self.name+'.png'
super().save(*args, **kwargs)
This code return error [Errno 2] No such file or directory: 'img/qr_codes/somename.png'此代码返回错误 [Errno 2] No such file or directory: 'img/qr_codes/somename.png'
Im trying to use signal @receive but it isn't help for me我正在尝试使用信号@receive,但这对我没有帮助
Make sure that your created the mentioned directories ( img/qr_codes/
).确保您创建了上述目录 (
img/qr_codes/
)。 the method only creates file.该方法仅创建文件。 It cannot create a directory.
它不能创建目录。
Note: I think you missed a closing bracket )
after your f-string.注意:我认为您在 f 弦后遗漏了右括号
)
。 Is that the same in your code?在你的代码中是一样的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.