Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Dec. 3, 2005 14:44:29

P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Uploading a file


Hi,
Did anybody try to upload a file via Django?
Can provide any working example?
Thank you
Regards,
L.

Offline

#2 Dec. 4, 2005 09:23:55

J.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Uploading a file


In the admin it works fine (except images that didnt work for me so had
to switch to filefield for images). Havent tried in frontend.

Working example - just configure your settings (MEDIA_ROOT) and add
FileField to your model and you're done.

Offline

#3 Dec. 4, 2005 19:39:21

G.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Uploading a file


I've been using something like this. I'm not sure if it's the best way
to do this ( everyone else - please correct me ), but the main idea id
that the file info will be in request.FILES.

# TEMPLATE:

<form action="{{myapp}}/upload/" method="post"
enctype="multipart/form-data" >
File: <input type="file" name="file" id="file">
<input type="submit" value="Upload File">
</form>



# VIEW:
def save_file( request ):

import os.path

# where to store files. Probably best defined in settings.py
filepath = '/home/simon/files/'

# right, so 'file' is the name of the file upload field
filename = request.FILES
filetype = request.FILES

#the uploaded data from the file
data = request.FILES

# the full file path and name
fullfilepath = os.path.join( filepath, filename )

# clean up filenames & paths:
fullfilepath = os.path.normpath( fullfilepath )
fullfilepath = os.path.normcase( fullfilepath )

# try to write file to the dir.
try:
f = open( fullfilepath, 'wb' ) # Writing in binary mode for windows..?

f.write( data )
f.close( )
except:
# something went wrong

--Simon

Offline

#4 Dec. 5, 2005 18:48:03

P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Uploading a file


Simon,Thank you for your help.
I changed the view a little. I have

############
def UploadFile(request):
if request.POST:
print "We are in POST"
import os.path
filepath = '/Media/Static/'
filename = request.FILES
filetype = request.FILES
data = request.FILES
fullfilepath = os.path.join( filepath, filename )
fullfilepath = os.path.normpath( fullfilepath )
fullfilepath = os.path.normcase( fullfilepath )
f = open( fullfilepath, 'wb' )
f.write( data )
f.close( )
print "SUCCESS"
return HttpResponse("SUCCESS")
else:
print "We are in GET "
errors = new_data = {}
manipulator = users.AddManipulator()
form = formfields.FormWrapper(manipulator, new_data, errors)
t = template_loader.get_template("board/UploadFile")
c = Context(request, {'form': form})
return HttpResponse(t.render(c))
#################

and the form like this:

#############
<form action="." method="post" enctype="multipart/form-data">
<p>Choose a file to be loaded: <input type="file" name="file"
id="file"> <input
type="submit" value="Upload File"> </p>
</form>
#############

but now the problem is that the program will never go to request.POST
section.
But when I remove enctype="multipart/form-data" from the form, the
program will go to request.POST section but I will receive the error:
##############
Request Method: POST
Request URL:http://localhost:8082/Up/Exception Type: MultiValueDictKeyError
Exception Value: "Key 'file' not found in MultiValueDict {}"
Exception Location:
C:\PYTHON23\lib\site-packages\django\utils\datastructures.py in
__getitem__, line 73
#######

and the problem is with
filename = request.FILES ...

But why? Thank you for the reply
Regards,
L.

Offline

#5 Dec. 5, 2005 19:41:25

Eric W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Uploading a file


On Monday 05 December 2005 10:47 am, PythonistL wrote:
> <form action="." method="post" enctype="multipart/form-data">

Try reading this. It might help.
<http://code.djangoproject.com/wiki/NewbieMistakes>

Offline

#6 Dec. 6, 2005 07:39:52

P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Uploading a file


Eric,
Thank you for your reply.
I checked thehttp://code.djangoproject.com/wiki/NewbieMistakesand
corrected the form( I added "/" in action) like this

###########
<form action="./" method="post" enctype="multipart/form-data">
<p>Choose a file n to be loaded: <input type="file" name="file"
id="file"> <input
type="submit" value="Upload File"> </p>
</form>
##########
but the result is the SAME.
The program will never go through the request.POST
section of the view. But why??
Any help would be appreciated.
Thank you
L.

Offline

Board footer

Moderator control

Enjoy the 22nd of January
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support