Django自定义图像上传字段与动态路径 [英] Django Custom image upload field with dynamic path

查看:259
本文介绍了Django自定义图像上传字段与动态路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建自己的字段类型来上传图像,它不会在django管理面板中使用。我想象的语法可能会像:

I'm trying to create my own field type for uploading images, it won't be used in the django admin panel. I imagine the syntax would probably look something like:

photo = CustomImageField(upload_to='images', width="200", height="200")

我想在上传时能够设置字段中的参数并保存图像。我已经查看了django文档,但我找不到任何关于如何做到这一点。所以基本上,我想要控制上传图像的位置以及上传表单被处理并保存对象时的宽度和高度参数。

I want to be able to set the parameters in the field when I upload and save an image. I've looked through the django documentation but I can't find anything about how to do this. So basically, I want to be able to control where the image is uploaded to and the width and height parameters when the upload form is processed and the object is saved.

我正在使用Django 1.2

I am using Django 1.2

任何帮助将不胜感激:)

Any help would be greatly appreciated :)

推荐答案

您可以使用location属性更改FileField的存储。

Your can change the storage of a FileField using the location property.

form_name.photo.storage.location = upload_dir_path      

它适用于FileField,也许如果您继承Django的ImageField,它也将适用于您的自定义图像字段。

It works for the FileField and maybe it will also work for your custom image field if you inherit the Django's ImageField.

这篇关于Django自定义图像上传字段与动态路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆