Newbie: do I need to upload in order to access data?

38 views
Skip to first unread message

Bob Aalsma

unread,
Aug 10, 2013, 10:52:16 AM8/10/13
to django...@googlegroups.com
Hi,

I'm trying to achieve the following:
  • user indicates a file on his/her machine 
  • the program opens the file, reads the data and acts on that

So far, I can find examples of indicating the file on the user's machine, but this is always combined with saving to database (which I don't want); the clearest example I could find is http://stackoverflow.com/questions/5871730/need-a-minimal-django-file-upload-example

Question 1: is it really necessary to store the data in my database?

If not, I've not been able to find how to actually open and read the file.
I've been trying out variations on reading, based on what I could find in the Tutorials and Managing files (https://docs.djangoproject.com/en/1.5/topics/files/ ) but I don't seem to understand how to actually find the path and filename the user would have indicated. I seem to get completely lost in FileField and FieldFile and connected methods <sigh>

Question 2: how do I find the indicated path and filename from the user?

Regards,
Bob

Nigel Legg

unread,
Aug 10, 2013, 12:39:38 PM8/10/13
to django...@googlegroups.com
I've based my process a minimal file upload - I think based on the answer to the link above.  I use:
models.py:
class Document(models.Model):
    docfile = models.FileField(upload_to='documents/%Y/%m/%d')

views.py:
def list(request):
    # Handle file uploadf
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'])
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('myproject.myapp.views.list'))
    else:
        form = DocumentForm() # A empty, unbound form

    # Load documents for the list page
    documents = Document.objects.all()

    # Render list page with the documents and the form
    return render_to_response(
        'myapp/list.html',
        {'documents': documents, 'form': form},
        context_instance=RequestContext(request)
    )

forms.py:
class DocumentForm(forms.Form):
    docfile = forms.FileField(
        label='Select a file',
        help_text='max. 42 megabytes'
    )

This saves the file in 'media/documents/2013/08/10/datafile.csv'.  You can then access this using the normal open() and read() functions, pointing them to the correct directory and file.  As far as I can see, the data remains in the file you upload, but the location and name are stored in the database - in this case, "documents/2013/10/08/datafile.csv".

Hope this helps

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Bob Aalsma

unread,
Aug 10, 2013, 1:04:02 PM8/10/13
to django...@googlegroups.com
Thanks Nigel, this looks very promising ;)

If I interpret your text correctly, this is based on the upload/save example I mentioned.
I'm really interested in the meaning of parts your closing paragraph:
  • "This saves the file in 'media/documents/2013/08/10/datafile.csv'." - I think this means the contents of the file is copied from the user disk to my disk, right? 
  • "pointing them to the correct directory and file." - could I not simply point to the original directory and file (on the user's machine) and read the contents from that location?

Regards,
Bob

Op zaterdag 10 augustus 2013 18:39:38 UTC+2 schreef Nigel Legg:

Nigel Legg

unread,
Aug 10, 2013, 6:10:01 PM8/10/13
to django...@googlegroups.com
The file is uploaded - ie it is copied onto the server drive. 
I don't think you can do stuff client side with Django - beyond my knowledge.

Bob Aalsma

unread,
Aug 11, 2013, 4:35:32 AM8/11/13
to django...@googlegroups.com
Thanks Nigel.

OK, at least this has stopped me running around in circles and I can continue from here.

I would think you can do stuff client side, but this is probably more Python and less Django - to be solved in future releases ;)

Regards,
Bob

You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/oX4xRqokJPQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

DJ-Tom

unread,
Aug 11, 2013, 5:56:24 AM8/11/13
to django...@googlegroups.com
Hi Bob



I'm trying to achieve the following:
  • user indicates a file on his/her machine 
  • the program opens the file, reads the data and acts on that


What you want to do is not possible - at least not without client-side scripting.

The "program" you refer to is running on your web server, not on the machine of the user.

So before your code can do anything to the users file, it needs to be transferred (uploaded) to your server. So there is nothing you can do with Django/Python before that.

After that processing, you may give the user a result page with a link to the processed file - but from your description I don't know if if this is what you want to do.

regards
Thomas

Bob Aalsma

unread,
Aug 11, 2013, 6:02:17 AM8/11/13
to django...@googlegroups.com
Hi Tom,

OK, thanks.

The file I'd want to read is a parameter file which contains user specific information. I just need to know the data to guide the actions, no need to store or keep it.
So I'll just accept the upload, extract the data and then delete the upload.
Oh well ;)

Regards,
Bob

mulianto

unread,
Aug 11, 2013, 3:45:30 PM8/11/13
to django...@googlegroups.com
Hi,

Why not try use JavaScript file open to process the Information in client.

It'a a waste of bandwith in client + server side if upload not needed.

Regards,

Mulianto
Http://muliantophang.blogspot.com



Sent from my iPhone
Reply all
Reply to author
Forward
0 new messages