List Info

Thread: Re: How can I rename uploaded files to a random filename?




Re: How can I rename uploaded files to a random filename?
user name
2007-06-26 22:43:13
On 6/25/07, rob <robrawb.net> wrote:
>
> We upload all of our images via the Admin app and would
like all
> uploaded images to be renamed to a set of numbers. We
can generate the
> random numbers fine, but is there an easy way to rename
the file once
> it's uploaded in the Admin app?

Not at present. You would need to write a customized
FileField to
implement this sort of behaviour.

Yours,
Russ Magee %-)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: How can I rename uploaded files to a random filename?
country flaguser name
United States
2007-06-27 01:51:27
If you just want to upload some files you might do something like the following. The save function uses plain python to save the files to the FILE_UPLOAD_DIR you defined in your setting file. You can play around with the file names as much as you want here.

Question: Why a random name?

Vincent

class Application_files(forms.Form):
 &nbsp; &nbsp;cv = forms.Field(label = 'CV *', widget = forms.FileInput(), help_text="pdf or Word document&quot;)
&nbsp; &nbsp; paper = forms.Field(label = 'Paper *', widget = forms.FileInput(), help_text="pdf or Word document&quot;)
&nbsp; 
def save(file,key,user,ext,overwrite=True):
&nbsp;   ;if ext in ['pdf','doc']:
&nbsp; &nbsp; &nbsp; &nbsp; f = open('%s%s_%s.%s' % (settings.FILE_UPLOAD_DIR,key,user,ext,),'wb')
 ; &nbsp; &nbsp; &nbsp; f.write(file)
 &nbsp;   ; &nbsp; f.close()

def apply(request):
&nbsp; &nbsp; if request.POST:
 ; &nbsp;   &nbsp; for i in request.FILES.keys():
 &nbsp; &nbsp;   ; &nbsp;  &nbsp;ext = request.FILES[i]['filename'][-3:].lower()
 &nbsp; &nbsp; &nbsp; &nbsp;   ; file = request.FILES[i]['content']
&nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp;save(file,i,request.user,ext.lower())
  ;   &nbsp; else:
   ; &nbsp; &nbsp; &nbsp; &nbsp; form = Application_files()

 &nbsp;  return render_to_response('apply/apply.html',{'form':form,})



On 6/26/07 10:43 PM, "Russell Keith-Magee" <freakboy3742gmail.com&gt; wrote:

>
> On 6/25/07, rob <robrawb.net&gt; wrote:
>>
>>; We upload all of our images via the Admin app and would like all
>> uploaded images to be renamed to a set of numbers. We can generate the
>> random numbers fine, but is there an easy way to rename the file once
>;> it's uploaded in the Admin app?
>
> Not at present. You would need to write a customized FileField to
> implement this sort of behaviour.
>
> Yours,
&gt; Russ Magee %-)
>
> >

--
Vincent R. Nijs
Assistant Professor of Marketing
Kellogg School of Management, Northwestern University
2001 Sheridan Road, Evanston, IL 60208-2001
Phone: +1-847-491-4574 Fax: +1-847-491-2498
E-mail: v-nijskellogg.northwestern.edu
Skype: vincentnijs


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users"; group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to django-users-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )