Tôi là người mới dùng django và python. Cần một số hướng dẫn trong nhiệm vụ này.django tải xuống tệp csv bằng liên kết
Trường hợp: Khi người dùng nhấn nút gửi trên biểu mẫu, trang sẽ hiển thị trang Thành công và liên kết nơi họ có thể tải xuống kết quả. Kết quả là trong tập tin excel. Tôi có thể tạo đầu ra để excel tập tin bằng cách sử dụng mô-đun xlwt và hiển thị các trang thành công riêng lẻ nhưng không phải cả hai cùng một lúc.
Tôi có gì: Tôi đang chạy django1.1.1 trên Windows XP với python 2.6. Có câu hỏi tương tự được yêu cầu nhưng không thể làm cho nó hoạt động.
page.html thành công của tôi có dòng này
<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
Khi tôi bấm vào liên kết, nó mang lại cho lỗi đường dẫn
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 là ở cả những vị trí C: /example.xls và trong thư mục tĩnh
Cấu trúc:
- webdb
- tĩnh
- example.xls
- Webinput
- urls.py
- views.py
- models.py
- tĩnh
Tôi có những 2 module là tốt. Nếu tôi sử dụng backup_to_csv nó hoạt động tốt nhưng nó downlods trực tiếp mà không có liên kết. Làm thế nào để làm tương tự khi tôi đã có một tập tin. Nếu có những cách khác mà tôi không phải lưu trữ tập tin, đó là tốt quá.
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 (yêu cầu, hàng):
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
Cảm ơn, nhưng nó mang lại cho lỗi này Traceback (cuộc gọi gần đây nhất cuối cùng): File "C: \ Python26 \ lib \ site-packages \ django \ core \ server \ basehttp.py", dòng 280, trong chạy self.finish_response() file "C: \ Python26 \ lib \ site-packages \ django \ core \ server \ basehttp.py", dòng 319, trong finish_response cho dữ liệu trong self.result: file "C : \ Python26 \ lib \ site-packages \ django \ http \ __ init__.py ", dòng 378, trong tiếp theo chunk = self._iterator.next() Tệp" C: \ Python26 \ lib \ site-packages \ django \ core \ servers \ basehttp.py ", dòng 50, trong tiếp theo dữ liệu = self.filelike.read (self.blksize) LoạiError: số nguyên là bắt buộc – user234850