2009-12-18 14 views
7

का उपयोग कर सीएसवी फ़ाइल डाउनलोड करें मैं 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 

उत्तर

7

अब यह काम करता है लेकिन मुझे एक्सेल (.xls) से csv में फ़ाइल एक्सटेंशन बदलना पड़ा।

मेरे urls.py = url(r'^static/example.txt', send_file)
मेरे HTML लिंक = <a href="../static/example.txt">Download CSV File</a>
मेरे view.py

def send_file(request): 

    import os, tempfile, zipfile 
    from django.core.servers.basehttp import FileWrapper 
    from django.conf import settings 
    import mimetypes 

    filename  = "C:\ex2.csv" # Select your file here. 
    download_name ="example.csv" 
    wrapper  = FileWrapper(open(filename)) 
    content_type = mimetypes.guess_type(filename)[0] 
    response  = HttpResponse(wrapper,content_type=content_type) 
    response['Content-Length']  = os.path.getsize(filename)  
    response['Content-Disposition'] = "attachment; filename=%s"%download_name 
    return response 
2

अपने URL में।py परिवर्तन

urls.py url(r'^static/(?P.*)$', send_file) 

को
urls.py url(r'^static/example.xls$', send_file) 

पहले एक में, आप भी एक और पैरामीटर के रूप में देखने के लिए/के बाद सब कुछ से गुजर रहे हैं, लेकिन आपके विचार इस पैरामीटर को स्वीकार नहीं करता। एक और विकल्प ध्यान में रखते हुए इस पैरामीटर स्वीकार करने के लिए किया जाएगा:

def send_file(request, path): 
    ... 

लेकिन जब से अपने xls फ़ाइल का पथ कठिन कोडित है, मुझे नहीं लगता कि आपको लगता है कि जरूरत नहीं है।

+0

धन्यवाद, लेकिन यह इस त्रुटि 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

1

टिप्पणी Ofri Raviv में। आपको लगता है कि अपने आप दे रही है एक

TypeError: an integer

है जो क्योंकि जबकि FileWrapper बनाने यू जिसमें से दो पैरामीटर गुजर रहे हैं एक दूसरे [वैकल्पिक] पूर्णांक माना जाता है लेकिन यू 'RB'

wrapper = FileWrapper(file(filename),"rb")

पारित कर दिया उल्लेख किया

कौन वास्तव में के रूप में लिखा जाना चाहिए FileWrapper (फ़ाइल (फ़ाइल नाम, "rb"))

आवरण =

तो यह सिर्फ एक misali था ('RB' फ़ाइल पैरामीटर है) ब्रेसिज़ का गुस्सा, लेकिन कभी-कभी डीबग करना मुश्किल हो जाता है। फ़ाइल:

संबंधित मुद्दे