Я новичок в django и python. Нужна помощь в этом поиске.
Случай: когда пользователь нажимает кнопку отправки в форме, должна отображаться страница успеха и ссылка, по которой он может загрузить результаты. Результаты в файле excel. Я могу создать вывод в файл excel с помощью модуля xlwt и отобразить страницу успеха по отдельности, но не одновременно.
Что у меня есть: я запускаю django1.1.1 на Windows XP с python 2.6. Был задан аналогичный вопрос, но он не смог заставить его работать.
моя страница успеха page.html имеет эту строку
<a href="../static/example.xls">Download CSV File</a>
URL.py:
url(r'^static/(?P<path>.*)$', send_file),
просмотров.py:
def send_file(request):
import os, tempfile, zipfile
from django.core.servers.basehttp import FileWrapper
"""
Send a file through Django without loading the whole file into
memory at once. The FileWrapper will turn the file object into an
iterator for chunks of 8KB.
"""
filename = "C:/example.xls" # Select your file here.
wrapper = FileWrapper(file(filename),"rb")
response = HttpResponse(wrapper, content_type='text/plain')
#response['Content-Length'] = os.path.getsize(filename)
return response
Когда я нажимаю на ссылку, она выдает ошибку пути
send_file() got an unexpected keyword argument 'path'
Request Method: GET
Request URL: localhost:8000/webinput/static/example.xls
Exception Type: TypeError
Exception Value:
send_file() got an unexpected keyword argument 'path'
Кстати, example.xls находится как в папке C:/example.xls, так и в статической папке.
Структура:
- webdb
- Static
- example.xls
- Webinput
- urls.py
- просмотры.py
- модели.py
- Static
У меня тоже есть эти 2 модуля. Если я использую backup_to_csv, он работает нормально, но загружается напрямую без ссылки. Как сделать то же самое, когда у меня уже есть файл. Если есть другие способы, где мне не нужно хранить файл, это тоже нормально.
защита xls_to_response (xls, fname):
response = HttpResponse(mimetype="application/ms-excel")
response['Content-Disposition'] = 'attachment; filename=%s' % fname
xls.save(response)
return response
def backup_to_csv (запрос, строка):
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename="backup.csv"'
writer = csv.writer(response, dialect='excel')
#code for writing csv file go here...
for i in row:
writer.writerow(i)
return response