Upload files without form

330 views
Skip to first unread message

kooliah

unread,
May 7, 2012, 7:19:08 PM5/7/12
to django...@googlegroups.com
I need to upload a list of images to a project.
I've found tons of pages about how to upload a file using a
form...(that's clearly written on django official docs) :-)

But i need to do it by a custom django-admin command because i have to
link theese files(900) to respective records in the project

I tryed with shutil.copy too, but it copies files and immediately delete
it. i saw them by file manager in the right folder, but after 1-2
seconds they disappeared.

So i have a list
f[1]='/path/to/source/image1.jpg'
f[2]='/path/to/source/image2.jpg'
f[3]='/path/to/source/image3.jpg'
.
.
f[n]='/path/to/source/imagen.jpg'

and i need to upload to

os.path.join(settings.MEDIA_ROOT, 'images')

Does someone already done it WITHOUT USING FORMS, or can help me...

Thanks to all

Alkatron

Bolang

unread,
May 7, 2012, 9:11:44 PM5/7/12
to django...@googlegroups.com
On 05/08/2012 06:19 AM, kooliah wrote:
>
> Does someone already done it WITHOUT USING FORMS, or can help me...

Hi,
I use this method
http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file

>
> Thanks to all
>
> Alkatron
>

kooliah

unread,
May 8, 2012, 12:53:56 PM5/8/12
to django...@googlegroups.com
As often happens to me, things are much less complicated than i figure out

I loose hours running after UploadedFile, HttpRequest, etc.
Finally, it was my fault, a wrong path

Using os.path.join(settings.MEDIA_ROOT, 'images') create a new tree from
static folder

Using os.path.join(settings.MEDIA_URL, 'images') return '/static/images'
that gives error (path does not exist) while using 'static/images' it
works now


(it's a test command, in real example i will load name from a file
passed by args)
------------------------------------------------------------------------------------------------------------------------------
from django.core.management.base import BaseCommand, CommandError
import os

ffiles = []
ffiles.append('/mnt/hostdir/project/fotoalkatron/SNC110.jpg')
ffiles.append('/mnt/hostdir/project/fotoalkatron/SNC111.jpg')
ffiles.append('/mnt/hostdir/project/fotoalkatron/SNC112.jpg')

class Command(BaseCommand):
def handle(self, *args, **options):
_upload_img()
def _upload_img(fname):
for fpathname in ffiles:
fname = os.path.split(fpathname)[1]
_dst= os.path.join( 'static/images', fname)
myfl = open(fpathname,'r')
cntn = myfl.read()
myfl.close()
fdst = open(_dst,'wb')
fdst.write(cntn)
fdst.close()
-------------------------------------------------------------------------------------------------------------------------------
Reply all
Reply to author
Forward
0 new messages