I am using django tables 2 and trying to export my table following the official documentation

余生颓废 提交于 2019-12-11 03:37:30

问题


I have followed the official documentation of django tables 2 but it is not working I dont know why

views.py

from django.shortcuts import render
from django_tables2 import RequestConfig
from django.template.response import TemplateResponse
from .models import Customer
from .tables import CustomerTable
from .tables import TableView
from django_tables2.export.export import TableExport

def user_profile(request):

    table= CustomerTable(Customer.objects.all())
    RequestConfig(request,paginate={'per_page':15}).configure(table)

    return render(request, 'home.html', {'table': table})


def TableView(request):
    table = CustomerTable(Customer.objects.all())

    RequestConfig(request).configure(table)

    export_format = request.GET.get('_export', None)
    if TableExport.is_valid_format(export_format):
        exporter = TableExport(export_format, table)
        return exporter.response('table.{}'.format(export_format))

    return render(request, 'table.html', {
        'table': table
    })

my html template

{% extends 'base.html' %}
{% block content %}





{% load render_table from django_tables2 %}
{% load querystring from django_tables2 %}




<!doctype html>
<html>
<head>
    <title>List of Customers</title>

</head>
<body>

    {% querystring '_export'='csv' %}home

    {% for format in table.export_formats %}
<a href="{% querystring '_export'=format %}">
    download  <code>.{{ format }}</code>
</a>
    {% endfor %}

    <p margin-bottom:500px;>  </p>

    <div style="width: 2500px; height: 600px; overflow-y: scroll;">
        <div id ="users">
            <input class="search" placeholder="Search" />



    {% render_table table %}
    {% endblock %}
    </div>
    </div>

</body>

urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', user_profile, name="user_profile"),
    url(r'^$', TableView, name="TableView"),

]

tables.py

import django_tables2 as tables
from .models import Customer
from django_tables2.export.views import ExportMixin

from .models import Customer

class CustomerTable(tables.Table):
    class Meta:
        model = Customer
        template = 'django_tables2/bootstrap.html'

class TableView(ExportMixin, tables.SingleTableView):
    table_class = CustomerTable
    model = Customer
    template_name = 'django_tables2/bootstrap.html'

I have a feeling something is wrong with the way I have defined urls.py but i cannot figure out what is wrong


回答1:


There is a note in the documentation on exporting saying:

This example assumes you define a list of possible export formats on your table instance in attribute export_formats.

So you need to add the desired export formats to a list on your table like this:

class CustomerTable(tables.Table):
    export_formats = ['csv', 'xls']

    class Meta:
        model = Customer
        template = 'django_tables2/bootstrap.html'

You an also define the list in a context variable for your template, if you prefer that. There is no 'right' in this.




回答2:


So I also banged my head against the wall a little bit on this, and then in a moment of clarity it hit me, you need wrap the query string tag like this in the template file

<a href="{% querystring '_export'='csv' %}"> download as csv</a>


来源:https://stackoverflow.com/questions/47450399/i-am-using-django-tables-2-and-trying-to-export-my-table-following-the-official

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!