Help with Files

0 views
Skip to first unread message

Gerard M

unread,
Mar 5, 2007, 7:06:11 PM3/5/07
to Django users
Hello, im very new to django and python, I've read the tutorials and
the django beta book, but there is one thing I dont see covered and I
need to know hot to do it for a project, any help is welcome.
The main problem is that I dont know how can I upload a file to the
server to be procesed using some kind of form provided for django or
via html form, how can I do this? can anyone please give me some
detailed information or tell me where can I find a tutorial or
something please?, thanks for your time and replies.

carole...@gmail.com

unread,
Mar 5, 2007, 8:23:23 PM3/5/07
to Django users
I ended up using an old fashioned form to post files with standard
python code (inside of a view though), because I didn't like the way
django handled them. I can post a sample of that if you
like.... .....

Gerard M

unread,
Mar 5, 2007, 8:57:51 PM3/5/07
to Django users
I would love to see that code Carole thanks a lot, and if you used
some kind of class definition to use it with the database and the view
you created I would love to have a look at that one too as well,
thanks

On 5 mar, 14:23, "carole.zie...@gmail.com" <carole.zie...@gmail.com>
wrote:

carole...@gmail.com

unread,
Mar 5, 2007, 9:16:40 PM3/5/07
to Django users
Well... I'm honestly just treating the file name in the database as a
varchar field ... because after I upload my files... I rename them
with a uniqueid, and move them to specific directories based on that
ID...but anyhow... below is the code for the basic idea... I've parsed
out some of my project specific stuff (like renaming the files and
moving them) ... so as just to give you what I believe you are looking
for ...so hopefully I don't have any typos in here:

Models File:
from django.db import models

class ImageExtension(models.Model):
image_extension_id = models.AutoField(primary_key=True)
image_extension_name = models.CharField(maxlength=15)
image_extension_description = models.CharField(blank=True,
maxlength=90)
time_stamp = models.DateTimeField()
class Meta:
db_table = 'image_extension'

class Image(models.Model):
image_id = models.AutoField(primary_key=True)
image_file_uuid = models.CharField(blank=True, maxlength=255) #
the file name
image_extension = models.ForeignKey(ImageExtension) # the file
extension ... we store them separately for a specific reason..you
don't HAVE to do that
insert_time_stamp = models.DateTimeField(null=True, blank=True)
class Meta:
db_table = 'image'


View Code:
def getUploadDir():
return "path you want filesuploaded to"

def renderAddImage(request):
if request.POST or request.FILES:
new_data = request.POST.copy()
new_data.update(request.FILES)
if new_data.has_key('uploadfile'): #we are uploading
fileitem = new_data['uploadfile']['content']
filename = new_data['uploadfile']['filename']
# name they had for the file
if not fileitem: return
fout = file (os.path.join(getUploadDir(),
filename), 'wb')
fout.write (fileitem)
fout.close()

#get the file extension so we can make it
lower case
extpos = filename.find(".")
extlen = len(filename)
ext = ""
cnt = extpos+1
while cnt<extlen:
ext = ext + filename[cnt]
cnt = cnt + 1

#find the image extension id ... again only
needed if you want to know what extension it is for other purposes
imgext =
ImageExtension.objects.get(image_extension_name__iexact=ext)
newImg =
Image(image_extension=imgext,image_file_uuid=filename)
newImg.save()

return HttpResponseRedirect("URL you want them
sent to after file upload")

else:
# No POST, so we want a brand new form without any data or
errors.
errors = new_data = {}
return render_to_response('adddocument.html')


HTML Template Code:

<form method="post" enctype="multipart/form-data" action=".">
{% if failed %}<font class="error">*** {{ failed }}</font>{% endif %}
<table cellpadding=0 cellspacing=0 border=0 class=none>
<tr class="none"><td class="none">
File:<br><input type="file" id="uploadfile" name="uploadfile"></
td>
</tr>
</table>
<br><input type="submit" value="Save"/>
</form>


That's the general idea...if you have any other questions...feel free
to ask..or if anyone sees anything I'm doing .. .that can be done more
efficiently... feel free to point it out ;)

Reply all
Reply to author
Forward
0 new messages