Soy nuevo en django y python. Necesito alguna guía en esta búsqueda.django descarga el archivo csv usando un enlace
Caso: cuando el usuario pulsa el botón de enviar en un formulario, debe mostrar la página de éxito y un enlace donde pueden descargar los resultados. Los resultados están en archivo Excel. Puedo crear un archivo de salida a Excel usando el módulo xlwt y mostrar la página de éxito de forma individual, pero no ambas al mismo tiempo.
Lo que tengo: Estoy ejecutando django1.1.1 en Windows XP con Python 2.6. Hubo una pregunta similar al pero no fue posible.
mi éxito pagina.html tiene esta línea de
<a href="../static/example.xls">Download CSV File</a>
urls.py:
url(r'^static/(?P<path>.*)$', send_file),
views.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
Cuando hago clic en el enlace, se da error de ruta
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'
Por cierto example.xls es tanto a localizaciones C: /example.xls y en la carpeta estática
Estructura:
- WebDB
- estático
- example.xls
- Webinput
- urls.py
- views.py
- models.py
- estático
tengo estos 2 módulos también. Si uso backup_to_csv funciona bien, pero se descarga directamente sin el enlace. Cómo hacer lo mismo cuando ya tengo un archivo. Si hay otras formas en que no tengo que almacenar el archivo, eso también está bien.
xls_to_response def (xls, fname):
response = HttpResponse(mimetype="application/ms-excel")
response['Content-Disposition'] = 'attachment; filename=%s' % fname
xls.save(response)
return response
def backup_to_csv (solicitud, fila):
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
Gracias pero da este error Rastreo (llamada más reciente pasado): archivo "C: \ Python26 \ lib \ site-packages \ Django \ core \ servers \ basehttp.py", línea 280, en ejecución self.finish_response() archivo "C: \ Python26 \ lib \ site-packages \ Django \ core \ servers \ basehttp.py", línea 319, en finish_response de datos en self.result: archivo "C : \ Python26 \ lib \ site-packages \ django \ http \ __ init__.py ", línea 378, en el siguiente chunk = self._iterator.next() Archivo" C: \ Python26 \ lib \ site-packages \ django \ core \ servers \ basehttp.py ", línea 50, en el siguiente data = self.filelike.read (self.blksize) TypeError: se requiere un número entero – user234850