2016-02-01 119 views
0

使用im Django的報表生成器和IM嘗試導出的我創建的報表爲XLS文件之一,這是我在views.py功能:出口Django的報告生成的.xls從模板報告

from django.shortcuts import render, get_object_or_404 
from django.http import HttpResponseRedirect 
from django.views.generic import View 
from django.core.urlresolvers import reverse 
from django.contrib.auth.models import User 
from django.shortcuts import render_to_response 
from django.template import RequestContext 
from django.utils import timezone 
from ..form import UsersForm, CostItemsForm, GroupsForm, SubGroupsForm, SubGroups2Form, CostElementsForm, RequestsForm 
from ..models import Groups, CostItems, Requests, CostElements, Positions, ItemChangeLogs, ElementChangeLogs 
from datetime import date, datetime 
from report_builder.models import Report 
from report_utils.mixins import DataExportMixin, generate_filename 


class DownloadFileView(DataExportMixin, View): 


    def dispatch(self, *args, **kwargs): 
     return super(DownloadFileView, self).dispatch(*args, **kwargs) 

    def process_report(self, report_id, user_id, 
         file_type, to_response, queryset=None): 
     report = get_object_or_404(Report, pk=2) 
     user = request.user 
     if not queryset: 
      queryset = report.get_query() 

     display_fields = report.get_good_display_fields() 

     objects_list, message = self.report_to_list(
      queryset, 
      display_fields, 
      user, 
      preview=False,) 
     title = re.sub(r'\W+', '', report.name)[:30] 
     header = [] 
     widths = [] 
     for field in display_fields: 
      header.append(field.name) 
      widths.append(field.width) 

     if to_response: 
       return self.list_to_xlsx_response(
        objects_list, title, header, widths) 

因此,即時調用從模板這樣的觀點:

{% extends 'cost_control_app/base_cost_control_app.html' %} 
{% block contentsubbase %} 
{% load widget_tweaks %} 

<div class="center-panel"> 
<h3>Reports</h3> 
     {% csrf_token %} 
     <br> 
     <div class="table-responsive"> 
      <a href="{% url 'cost_control_app:report_download' %}" >Export</a> 
     </div> 
</div> 
{% endblock contentsubbase %} 

這是我的urls.py連接所有:

from django.conf.urls import include, url, patterns 
from django.contrib.auth import views as auth_views 
from django.contrib.auth.views import login, logout 
from .views import views 

urlpatterns = patterns(
    '', 
    url(r'^reports/download/$', views.DownloadFileView.as_view(), name = "report_download"), 
) 

事情是,不工作,當我點擊一個名爲「導出」的標籤,它把我帶到空模板,但沒有保存文件對話框或沒有....請任何想法?

在此先感謝

回答

0

發現它(的人誰一段具有相同的問題),我缺少的DEF得到上面的參數,其中一套,這是更新的views.py,把一些默認值作爲一個例子:

class DownloadFileView(DataExportMixin, View): 


    def process_report(self, report_id, user_id, 
         file_type, to_response, queryset=None): 
     report = get_object_or_404(Report, pk=report_id) 
     user = User.objects.get(pk=user_id) 
     if not queryset: 
      queryset = report.get_query() 

     display_fields = report.get_good_display_fields() 

     objects_list, message = self.report_to_list(
      queryset, 
      display_fields, 
      user, 
      preview=False,) 
     title = re.sub(r'\W+', '', report.name)[:30] 
     header = [] 
     widths = [] 
     for field in display_fields: 
      header.append(field.name) 
      widths.append(field.width) 

     if to_response: 
      return self.list_to_xlsx_response(
       objects_list, title, header, widths) 


    def get(self, request, *args, **kwargs): 
     report_id = 2 
     file_type = 'xls' 
     user_id = 2 
     return self.process_report(
      report_id, user_id, file_type, to_response=True)