Re: Newbie: do I need to upload in order to access data?
Thanks Nigel, this looks very promising ;)
Op zaterdag 10 augustus 2013 18:39:38 UTC+2 schreef Nigel Legg:
-- 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:
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 helpsRegards,
Nigel Legg
07914 740972
http://www.trevanianlegg.co.uk
http://twitter.com/nigellegg
http://uk.linkedin.com/in/nigellegg On 10 August 2013 15:52, Bob Aalsma <overhaalsg...@me.com> wrote: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
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 .
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home