0
我正在嘗試設置Cloudinary的上傳控件,並在我的模型上將生成的Cloudinary對象保存到我的CloudinaryField()
。 Cloudinary的sample project只顯示如何使用{{form}}
上傳圖像。Django - 通過上傳控件保存Cloudinary對象
當我使用小部件上傳時,我收到了一本字典,但在文檔中我找不到任何可以保存它的地方。
我正在嘗試設置Cloudinary的上傳控件,並在我的模型上將生成的Cloudinary對象保存到我的CloudinaryField()
。 Cloudinary的sample project只顯示如何使用{{form}}
上傳圖像。Django - 通過上傳控件保存Cloudinary對象
當我使用小部件上傳時,我收到了一本字典,但在文檔中我找不到任何可以保存它的地方。
上傳小部件可以選擇包含通知URL,並且可以在您的上傳預設設置中進行配置。此通知網址將返回可用於保存圖像模型的服務器端點響應。
要從上傳響應中生成必需的CloudinaryField,可以使用CloudinaryResource對象。
from cloudinary import CloudinaryResource
. . .
json_response = { "public_id": "test", "type": "upload", . . . }
# Populate a CloudinaryResource object using the upload response
result = CloudinaryResource(public_id=json_response['public_id'], type=json_response['type'], resource_type=json_response['resource_type'], version=json_response['version'], format=json_response['format'])
str_result = result.get_prep_value() # returns a CloudinaryField string e.g. "image/upload/v123456789/test.png"
# Save the result
p = Photo(title="title",image=str_result)
p.save() # save result in database
「字典」,你的意思是標識符字符串是生成的隱藏輸入字段的值,或POST請求返回的JSON響應? –
這是由POST請求返回給Cloudinary的一組值。我想這是JSON,但是我直接將它發送到後端,並且Python似乎將其識別爲字典。也許我跳了一步? –