Thanks for helping.
I have a view that allows me to upload a csv file and to read it. I want this file’s data to be put in one of my template (and hence on a webpage). My view looks like this:
@login_required
def uploadFunc(request, username):
user = get_object_or_404(User, username=username)
if request.method == 'GET':
return render_to_response('upload.html',{'user':user},context_instance=RequestContext(request))
elif request.method == 'POST':
with open('penguins.csv', 'rb') as f:
reader = csv.reader(f)
for m in reader:
print m
return HttpResponseRedirect("/")
As you can see from
print m
return HttpResponseRedirect("/")
I print the data extracted (it gets printed to terminal, but not a webpage), and then redirect the upload page to my home page where a table out of the penguin.csv (don’t laugh at the name! I know:)) should be constructed. Thing is I do not understand (or know) how do I let my template index.html to know that it is that file that should be put in that table.
Sorry for a lot of talk, if it is confusing feel free to ask questions.
Thanks again,
blargie-bla
You need to first upload the file and save it into disk (or memory), then you need to save the this state for the next request. You have a few methods of doing that, for example in the session.
Here’s an example, it’s not tested and might have some errors…
Then in the template for index.html you can use the file_lines to populate the table.
I “glued” this code from various snippets I found just now, here’s the list of urls: