从模板导出Django报告生成器.xls报告

时间:2016-02-01 21:50:47

标签: python django reporting

我正在使用Django报告构建器,我试图将我创建的一个报告导出为xls文件,这是我在views.py中的功能:

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"),
)

事情是,不起作用,当我点击一个名为“导出”的标签时,它将我带到空模板但没有保存文件对话框或什么都没有....有什么想法吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

找到它(对于任何时候有同样问题的人),我错过了def get where where where where where,这是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)