On 5 mar, 14:23, "carole.zie...@gmail.com" <carole.zie...@gmail.com>
wrote:
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 ;)