我正在使用 sorl 的 ImageWithThumbnailsField 来处理我的项目中的图像,没有任何问题。现在我想使用 PIL 从我的文本中生成图像,在某些情况下,用户不会上传照片。我很难弄清楚代码是什么,所以任何帮助都将不胜感激!
步骤 1. 当用户不上传照片时,使用 PIL 创建一张(完成)
步骤 2. 将创建的照片指定为 ImageWithThumbnailsField(您的帮助在此处)
谢谢!
我正在使用 sorl 的 ImageWithThumbnailsField 来处理我的项目中的图像,没有任何问题。现在我想使用 PIL 从我的文本中生成图像,在某些情况下,用户不会上传照片。我很难弄清楚代码是什么,所以任何帮助都将不胜感激!
步骤 1. 当用户不上传照片时,使用 PIL 创建一张(完成)
步骤 2. 将创建的照片指定为 ImageWithThumbnailsField(您的帮助在此处)
谢谢!
如果没有您当前正在做什么的例子,很难说,但这是我认为应该起作用的。
# retrieve the Model instance however your app requires
m = YourModel.objects.get(pk=1)
# retrieve the upload_to path from the Field definition
upload_to = m.img_with_thumb.field.upload_to
# update field with location of the new image
# img_filename should be whatever PIL created in step 1
m.img_with_thumb = os.path.join(upload_to, img_filename)
# save the model
m.save()
我从来没有用 sorl 试过这个,但它适用于普通的 Django ImageField。