1

I'm new to Django, and Django Admin, so I'm still not 100% with the correct names for things.

I want to add a "Download All" button/link/action to the list view when using the Admin app for a Model. Ideally just like the "Add" button. I tried a custom action, which almost got me there, but it is insisting I select at least one row before it will trigger.

I'm very focused on delivering results fast here - as long as I can explain to my users how to use the app - then job done.

I'm using Django 1.9.

1
  • You would need to override the django admin change_list.html and keep a copy of this in you app/templates/app folder to add an button or link. Than this link should have url and callable to trigger an action. Hope this help Commented May 4, 2016 at 7:09

1 Answer 1

2

step1: First override the change_list.html which is at /app/templates/admin/app/change_list.html.(if it's not there create one)

The contents of change_list.html will look like

{% block object-tools %}
    {% if has_add_permission %}
        <ul class="grp-object-tools">
            {% block object-tools-items %}
                <li><a href="export/" class="grp-state-focus">
                    Export Data
                </a></li>
                <li><a href="add/{% if is_popup %}?_popup=1{% endif %}" class="grp-add-link grp-state-focus">
                    {% blocktrans with cl.opts.verbose_name as name %}Add {{ name }}{% endblocktrans %}
                </a></li>
            {% endblock %}
        </ul>
    {% endif %}
{% endblock %}

step2: In admin.py for this form add get_urls method

def get_urls(self):
    urls = super(classname, self).get_urls() # replace wit your class name
    my_urls = patterns(
        '',
        url(r'^export/$',
            ExportCampaignView.as_view(), name='export'),
    )
    return my_urls + urls

step3: In views.py add this logic to download the file

XLSX_CONTENT_TYPE = ('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')

class ExportCampaignView(generic.View):

    def get(self, request, *args, **kwargs):
        xlsx_file = self.create_xlsx()
        response = HttpResponse(xlsx_file, content_type=XLSX_CONTENT_TYPE)
        response['Content-Disposition'] = (
            'attachment; filename=campaign.xlsx')
        return response

    @staticmethod
    def create_xlsx():
        """
        :return tablib.Dataset: Campaign data.
        """
        output = StringIO()
        workbook = xlsxwriter.Workbook(output)
        sheet = workbook.add_worksheet()
        bold_with_bg = workbook.add_format(
            {'bold': True, 'bg_color': 'silver'})
        date_format = workbook.add_format({'num_format': 'dd.mm.YYYY'})

        headers = [
            _(u'col1'), _(u'col2'), _(u'col3'),

        ]


        data = Model.objects.all()

        for row_idx, row in enumerate(data, start=1):
            sheet.write(row_idx, 0, row.col1)
            sheet.write(row_idx, 1, row.col2)
            sheet.write(row_idx, 1, row.col3)

        workbook.close()
        output.seek(0)
        return output
Sign up to request clarification or add additional context in comments.

1 Comment

Nice, just change the grappelli classes (grp-add-link), to the regular django-admin ones, and logic can be in the admin class not the view :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.