跳转到主要内容

使用django-tables2-reports,您可以通过对项目进行最少的修改,获取任何django-tables2的报告(CSV,XLS)

项目描述

django-tables2-reports

https://travis-ci.org/goinnn/django-tables2-reports.svg?branch=master https://badge.fury.io/py/django-tables2-reports.svg

使用django-tables2-reports,您可以通过对项目进行最少的修改,获取任何表格的报告(CSV,XLS)

需求

安装

  • 在您的设置中

INSTALLED_APPS = (

    'django_tables2_reports',
)


TEMPLATE_CONTEXT_PROCESSORS = (

    'django.core.context_processors.static',

)


# This is optional

EXCEL_SUPPORT = 'xlwt' # or 'openpyxl' or 'pyexcelerator'

项目更改

1.a 现在您的表格应扩展为‘TableReport’

############### Before ###################

import django_tables2 as tables


class MyTable(tables.Table):

    ...

############### Now ######################

from django_tables2_reports.tables import TableReport


class MyTable(TableReport):

    ...

1.b 如果您想从报告中排除某些列(例如,如果它是按钮列),则应设置‘exclude_from_report’ - 列名称(以及表格中的属性‘exclude’)

class MyTable(TableReport):

    class Meta:
        exclude_from_report = ('column1', ...)
    ...

2.a 如果您使用传统视图,现在您应使用其他RequestConfig并稍微修改您的视图

############### Before ###################

from django_tables2 import RequestConfig


def my_view(request):
    objs = ....
    table = MyTable(objs)
    RequestConfig(request).configure(table)
    return render_to_response('app1/my_view.html',
                              {'table': table},
                              context_instance=RequestContext(request))

############### Now ######################

from django_tables2_reports.config import RequestConfigReport as RequestConfig
from django_tables2_reports.utils import create_report_http_response

def my_view(request):
    objs = ....
    table = MyTable(objs)
    table_to_report = RequestConfig(request).configure(table)
    if table_to_report:
        return create_report_http_response(table_to_report, request)
    return render_to_response('app1/my_view.html',
                              {'table': table},
                              context_instance=RequestContext(request))

如果你在项目中有很多表格,你可以激活中间件,你不需要更改你的视图,只需要更改 RequestConfig 导入

# In your settings

MIDDLEWARE_CLASSES = (

    'django_tables2_reports.middleware.TableReportMiddleware',
)

############### Now (with middleware) ######################

from django_tables2_reports.config import RequestConfigReport as RequestConfig

def my_view(request):
    objs = ....
    table = MyTable(objs)
    RequestConfig(request).configure(table)
    return render_to_response('app1/my_view.html',
                              {'table': table},
                              context_instance=RequestContext(request))

2.b. 如果你使用 基于类的视图

############### Before ###################

from django_tables2.views import SingleTableView


class PhaseChangeView(SingleTableView):
    table_class = MyTable
    model = MyModel


############### Now ######################

from django_tables2_reports.views import ReportTableView


class PhaseChangeView(ReportTableView):
    table_class = MyTable
    model = MyModel

用法

在表格下方出现一个 CSV 图标(如果 Python 路径中有 xlwtopenpyxlpyExcelerator,则还会出现 XLS 图标),如果你点击此图标,你会得到一个包含表格中每个项目的 CSV 报告(或 xls 报告)(无分页)。排序正常工作!

开发

通过克隆其 git 仓库,你可以获取 django-tables2-reports 的最新版本

git clone https://github.com/goinnn/django-tables2-reports

测试项目

在源树中,你会找到一个名为“test_project”的目录。它包含一个使用 django-tables2-reports 的已设置好的项目。你可以像往常一样运行它

cd test_project
export PYTHONPATH=..
python manage.py syncdb --noinput
python manage.py runserver

发布

0.1.0 (2017-06-19)

  • 维护版本

  • Django 1.8、1.9、1.10、1.11 支持

  • 支持 openpyxl > 2.0.0

  • 支持最近 django-tables2

  • 新维护者 Michał Pasternak

0.0.10 (2014-10-13)

  • 针对 xlsx Content-Type 的修复
    • 当报告格式不被识别时,django-tables2-reports 抛出 500 服务器错误。在这种情况下,404 更为合适。

    • django-tables2-reports 为 xlsx 文件设置 Content-Type 为 application/vnd.ms-excel,这会在 Firefox 中引起警告。application/vnd.openxmlformats-officedocument.spreadsheetml.sheet 是 xlsx 正确的 Content-Type

  • 支持 Django 1.7(很抱歉延迟)

  • 添加新功能:exclude_from_report

  • 还有一些细节

  • 感谢

0.0.9 (2013-11-30)

  • 与 Django 的未来版本(>=1.7)兼容

  • 更新测试

  • 重构代码

  • 修复当工作表标题超过 31 个字符时的错误

  • 感谢

0.0.8 (2013-11-14)

0.0.7 (2013-08-29)

0.0.6 (2013-08-22)

0.0.5 (2013-07-03)

0.0.4 (2013-05-17)

  • 在输出期间正确转义 csv 数据

  • 现在带有逗号的字段不会被分割成多个列

  • 感谢

0.0.3 (2012-07-19)

  • 修复一个小的错误,当列中有换行符时。现在这些换行符被更改为了空格

  • 详情

0.0.2 (2012-07-18)

0.0.1 (2012-07-17)

  • 初始版本

项目详情


下载文件

下载适合你平台的文件。如果你不确定要选择哪个,了解更多关于 安装包 的信息。

源分布

django-tables2-reports-0.1.3.tar.gz (21.6 kB 查看哈希值)

上传时间 源码

构建发行版

django_tables2_reports-0.1.3-py2.py3-none-any.whl (34.6 kB 查看哈希值)

上传时间 Python 2 Python 3