i am following this post Python get file name and change & save it in variable and have make some changes which can be seen in this post Python get file name and change & save it in variable.
my quest is to show this changed .txt file in the HTML page either manually(inserting the path of the file in list.html) or Dynamically (passing the 'newfilename') to list.html.
too confused, how to give download option on html. both files are in the same folder. please advice
# -*- coding: utf-8 -*-
import sqlite3
import csv
import sys
import os
import xlsxwriter
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from myproject.myapp.models import Document
from myproject.myapp.forms import DocumentForm
def list(request):
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile = request.FILES['docfile'])
newdoc.save()
docfile = request.FILES['docfile']
filename = os.path.splitext(docfile.name)[0]
newfilename= 'ok_%s.xlsx' %filename
gfile= csv.reader(request.FILES['docfile'])
gon = sqlite3.connect('../myproject/database/database.sqlite3')
gon.text_factory = str
gon.execute("DELETE FROM test where rowID > 0 ")
gon.executemany('insert into test values (?, ?, ?)', gfile)
gon.commit()
gon.close()
conn=sqlite3.connect('../myproject/database/database.sqlite3')
conn.text_factory = str
c=conn.cursor()
ion=c.execute("delete from test where [Manager id] = 'local'")
.....
.....
conn.commit()
conn.close()
workbook = xlsxwriter.Workbook('../myproject/media/documents/'+newfilename)
worksheet = workbook.add_worksheet("no emp")
worksheet2 = workbook.add_worksheet("Manager id")
# 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().delete()
#documents = Document.objects.all()
#mocuments = '../myproject/media/documents/ok_2.xlsx' #this is what i am trying to do
# Render list page with the documents and the form
documents = Document.objects.all()
return render_to_response(
'myapp/list.html', {'documents': documents, 'form': form}, context_instance=RequestContext(request)
)