4

I am trying to build an admin action 'download_selected' which will download selected models. When the action is selected, I redirect to an intermediate page so that users can select a download format. When a user selects a download format and clicks on 'download', it downloads the file. But stays on the same intermediate page. How do I redirect it back to change form admin page? This redirection that I want is similar to django 'download selected file' default admin action. Thanks.

Here is my code.

admin.py

class SelectDownloadFormatForm(forms.Form):
    DOWNLOAD_TYPE_CHOICES=[('csv','csv'),
                           ('json', 'json'),
                           ('xml','xml')]

    _selected_action = forms.CharField(widget=forms.MultipleHiddenInput)
    download_type = forms.ChoiceField(label=_('Select a Download type'), choices=DOWNLOAD_TYPE_CHOICES, widget=forms.RadioSelect())


def download_selected(self, request, queryset):
    import csv
    from django.http import HttpResponse, HttpResponseRedirect
    import StringIO

    form = None

    if 'download' in request.POST:
        form = self.SelectDownloadFormatForm(request.POST)

        if form.is_valid():
            dtype = form.cleaned_data['download_type']
            print dtype
            response = HttpResponse(content_type='text/csv')
            response['Content-Disposition'] = 'attachment; filename="export.csv"'
            writer = csv.writer(response)
            writer.writerow(['id', 'name', 'qid' ,'label', 'name', 'field'])

            count = 0
            for s in queryset:
                questions_query = ParentModel.objects.filter(parent_form_id = s.id)
                for q in questions_query:
                    writer.writerow([s.id, s.name, q.id, q.label, q.name, q.field])
                    count += 1
          
            plural = ''
            if count != 1:
                plural = 's'

            self.message_user(request, "Successfully downloaded %d survey response%s in %s format" % (count, plural, dtype))               
            return response

    if not form:
        form = self.SelectDownloadFormatForm(initial={'_selected_action': request.POST.getlist(admin.ACTION_CHECKBOX_NAME)})

    return render(request,'admin/download_type.html', {'items': queryset,
                                                     'download_type_form': form,
                                                    })
download_selected.short_description = "Download selected forms"

download_type.html

{% extends "admin/base_site.html" %}
{% block content %}
<form action="" method="post">
  {% csrf_token %}  
  {{ download_type_form }}

  <p>Following survey will be downloaded with corresponding responses:</p>

  <ul>{{ items|unordered_list }}</ul>

  <input type="hidden" name="action" value="download_selected" />
  <input type="submit" name="download" value="Download" />
 </form>

{% endblock %}
4

2 回答 2

1

我添加了一个额外的按钮返回

<a href="#" onclick="window.history.back(); return false;" class="button cancel-link">Go Back</a>
于 2015-11-05T15:23:54.627 回答
0

您需要 javascript 进行重定向。

您可以使用jQuery File Download来执行以下操作:

$.fileDownload('/url/to/download').done(function {
        // redirect
})

不确定您是否可以将其与表单帖子结合使用。

于 2015-11-05T11:04:50.387 回答