upload_to for ImageField changes name of file being uploaded

44 views
Skip to first unread message

bradford li

unread,
Jan 29, 2015, 3:35:22 AM1/29/15
to django...@googlegroups.com
I posted a question on stackoverflow regarding my issue:


Basically upload_to value will change my ImageField.name to the upload_to value

bradford li

unread,
Feb 3, 2015, 2:22:13 AM2/3/15
to django...@googlegroups.com
There is bounty on this stackoverflow question is anyone is interested

Tom Evans

unread,
Feb 3, 2015, 7:23:24 AM2/3/15
to django...@googlegroups.com
On Tue, Feb 3, 2015 at 7:22 AM, bradford li <bradf...@gmail.com> wrote:
> There is bounty on this stackoverflow question is anyone is interested
>

It looks like it has already been answered multiple times - you cannot
upload a file in to a subdirectory of MEDIA_ROOT and have its name
attribute be the base file name. The name attribute of a FileField is
the path to the file relative to MEDIA_ROOT*. If you want to store
files in subfolders of MEDIA_ROOT, then obviously, the name attribute
will not consist of just the file name of the uploaded file.

If you want to know or use the name of the file, use
os.path.basename() on the name attribute.

Cheers

Tom

* This isn't actually true, but is close enough to the truth as makes
no difference. It is in fact relative to whatever "storage engine"
that FileField uses; the default storage engine behaves as I have
described.
Reply all
Reply to author
Forward
0 new messages