Ich bin neu in Django und Python. Brauche etwas Anleitung in dieser Quest.django herunterladen CSV-Datei mit einem Link
Fall: Wenn der Benutzer die Übermittlungsschaltfläche in einem Formular anklickt, sollte die Seite "Erfolg" und ein Link angezeigt werden, über den die Ergebnisse heruntergeladen werden können. Die Ergebnisse sind in Excel-Datei. Ich kann Ausgabe, um Excel-Datei mit Xlwt-Modul erstellen und zeigen Sie die Erfolgsseite einzeln, aber nicht beide gleichzeitig.
Was ich habe: Ich verwende django1.1.1 auf Windows XP mit Python 2.6. Es wurde eine ähnliche Frage gestellt aber war nicht in der Lage, es zum Laufen zu bringen.
mein Erfolg seite.html hat diese Linie
<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
Wenn ich auf den Link klicken, gibt es Pfadfehler
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'
0 BTW
example.xls ist an den beiden Standorten C: /example.xls und in statischen Ordner
Struktur:
- WebDB
- Static
- example.xls
- Webinput
- urls.py
- views.py
- models.py
- Static
Ich habe diese 2 als auch Module. Wenn ich backup_to_csv verwende, funktioniert es gut, aber es wird direkt ohne den Link heruntergeladen. Wie mache ich das Gleiche, wenn ich bereits eine Datei habe? Wenn es andere Möglichkeiten gibt, wo ich keine Datei speichern muss, ist das auch in Ordnung.
def 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 (Anfrage, Zeile):
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
Danke, aber es gibt diesen Fehler Traceback (jüngste Aufforderung zuletzt): Datei "C: \ Python26 \ lib \ site-packages \ django \ core \ Server \ basehttp.py", Zeile 280, in Lauf self.finish_response() Datei "C: \ Python26 \ lib \ site-packages \ django \ core \ server \ basehttp.py", Linie 319, in finish_response für Daten in self.result: Datei „C : \ Python26 \ lib \ site-packages \ django \ http \ __ init__.py ", Zeile 378, im nächsten chunk = self._iterator.next() Datei" C: \ Python26 \ lib \ site-packages \ django \ core \ servers \ basehttp.py ", Zeile 50, in nächsten data = self.filelike.read (self.blksize) TypeError: eine Ganzzahl ist erforderlich – user234850