का उपयोग कर सीएसवी फ़ाइल डाउनलोड करें मैं django और पायथन के लिए एक नया हूँ। इस खोज में कुछ मार्गदर्शन की आवश्यकता है।django एक लिंक
केस: जब उपयोगकर्ता किसी फॉर्म पर सबमिट बटन हिट करता है, तो उसे सफलता पृष्ठ और एक लिंक प्रदर्शित करना चाहिए जहां वे परिणाम डाउनलोड कर सकते हैं। परिणाम एक्सेल फ़ाइल में हैं। मैं xlwt मॉड्यूल का उपयोग कर एक्सेल फ़ाइल में आउटपुट बना सकता हूं और सफलता पृष्ठ को व्यक्तिगत रूप से प्रदर्शित कर सकता हूं लेकिन दोनों एक ही समय में नहीं।
मेरे पास क्या है: मैं विंडोज XP पर python 2.6 के साथ django1.1.1 चला रहा हूं। इसी तरह के प्रश्न से पूछा गया था लेकिन यह काम करने में सक्षम नहीं था।
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
जब मैं लिंक पर क्लिक करें, यह देता है
मेरी सफलता page.html इस लाइन
<a href="../static/example.xls">Download CSV File</a>
urls.py है पथ त्रुटि
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'
BTW example.xls दोनों स्थानों सेल्सियस पर है: /example.xls और स्थिर फ़ोल्डर में
संरचना:
- webdb
- स्टेटिक
- example.xls
- वेबिनपुट
- urls.py
- views.py
- models.py
- स्टेटिक
मैं भी इन 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
डीईएफ़ 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
धन्यवाद, लेकिन यह इस त्रुटि Traceback (सबसे हाल कॉल पिछले) देता है "C: \ Python26 \ lib \ साइट-संकुल \ Django \ कोर \ सर्वर \ basehttp.py", लाइन 280, समय में self.finish_response() फ़ाइल "C: \ Python26 \ lib \ साइट-संकुल \ Django \ कोर \ सर्वर \ basehttp.py", लाइन 319, finish_response में self.result में डेटा के लिए: फ़ाइल "सी : \ Python26 \ lib \ site-packages \ django \ http \ __ init__.py ", लाइन 378, अगले में chunk = self._iterator.next() फ़ाइल" सी: \ Python26 \ lib \ site-packages \ django \ core \ सर्वर \ basehttp.py ", लाइन 50, अगले डेटा = self.filelike.read (self.blksize) टाइप एरर: एक पूर्णांक आवश्यक है – user234850