0

I am trying to incorporate an export button into my Django admin site. Able to get the button to show in admin, but every time I click the button it breaks and I get the following error:

NoReverseMatch at /admin/emarshalapp/attorney/export/ Reverse for 'app_list' with arguments '()' and keyword arguments '{'app_label': ''}' not found. 1 pattern(s) tried: ['admin/(?Pfiler|emarshalapp|auth)/$']

Here is the full traceback in case that is helpful.

I am trying to follow this tutorial to do the export logic, but I obviously must be doing something wrong, just not sure what. I have tried all the solutions recommended for NoReverseMatch on SO (including this answer) and elsewhere but no fix in sight. I am stumped, please help!

The part of my change-list.html template that adds the button:

{% block object-tools-items %}

    {{ block.super }}

    <li>
        <a href="export/"
           class="grp-state-focus addlink">Export</a>
    </li>

{% endblock %} 

here is my INSTALLED_APPS setting:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # my apps here
    'emarshalapp',
    'localflavor',
    'simple_history',
    'easy_thumbnails',
    'filer',
    'mptt',
    'PIL',
    'django_extensions',

]

admin.py:

def my_view(self, request):
        # custom view which should return an HttpResponse
        if request.method == 'POST':
            if 'export' in request.POST:
                response = HttpResponse(
                    content_type='application/vnd.ms-excel')
                response['Content-Disposition'] = \
                    'attachment; filename=Report.xlsx'
                xlsx_data = WriteToExcel(attorney_range, attorney)
                response.write(xlsx_data)
                return response
        else:
            return render(request, "change_list.html",
                          context_instance=RequestContext(request))

def get_urls(self):
    urls = super(AttorneyAdmin, self).get_urls()
    my_urls = patterns('', url(r'^export/$', self.my_view, name='export/'))
    return my_urls + urls

views.py:

def attorney_history(request):
    attorney_range = Attorney.objects.all().filter(active=True)
    attorney = None
    if request.method == 'POST':
        if 'export' in request.POST:
            response = HttpResponse(content_type='application/vnd.ms-excel')
            response['Content-Disposition'] = \
                'attachment; filename=Report.xlsx'
            xlsx_data = WriteToExcel(attorney_range, attorney)
            response.write(xlsx_data)
            return HttpResponseRedirect('%s/export/' % reverse('export/'))
    else:
        return render("change_list.html",
                      context_instance=RequestContext(request))

urls.py:

app_name = 'emarshalapp'
...
    def get_urls(self):
        urls = super(AttorneyAdmin, self).get_urls()
        my_urls = url(r"^export/$",
                      name='export/')
        return my_urls + urls

urlpatterns = [
    url(r'^admin/', admin.site.urls, name='admin'),
    url(r'^advanced_filters/', include('advanced_filters.urls'))

Then I have an excel_utils.py as per the tutorial:

def WriteToExcel(attorney_range, attorney=None):
    output = StringIO.StringIO()
    workbook = xlsxwriter.Workbook(output)
    worksheet_s = workbook.add_worksheet("Summary")
[code to add excel data]

my AttorneyAdmin class:

@admin.register(Attorney)
class AttorneyAdmin(SimpleHistoryAdmin):
    change_list_template = 'change_list.html'

    def my_view(self, request):
        # custom view which should return an HttpResponse
        if request.method == 'POST':
            if 'export' in request.POST:
                response = HttpResponse(
                    content_type='application/vnd.ms-excel')
                response['Content-Disposition'] = \
                    'attachment; filename=Report.xlsx'
                xlsx_data = WriteToExcel(attorney_range, attorney)
                response.write(xlsx_data)
                return response
        else:
            return render(request, "change_list.html",
                          context_instance=RequestContext(request))
    fieldsets = (...)
    list_display = (...)
    inlines = (...)
    search_fields = (...)
    list_filter = (...)
Community
  • 1
  • 1
Nicole Marie
  • 159
  • 2
  • 12
  • You may need to show your other urls, since the one it appears to be trying to match is else where, it would appear as though the `app_label` value you're passing in is missing or an empty string – Sayse Aug 30 '16 at 14:16
  • Possible duplicate of [What is a NoReverseMatch error, and how do I fix it?](http://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i-fix-it) – Sayse Aug 30 '16 at 14:16
  • I updated my post to include my other url patterns. Not sure where I would find my app_label value and how I would make sure it matches what I am passing? – Nicole Marie Aug 30 '16 at 14:29
  • Where and when is this error popping up exactly? – Swakeert Jain Aug 30 '16 at 14:45
  • @SwakeertJain I get this error when I click the Export button which I added to the admin change list page. – Nicole Marie Aug 30 '16 at 14:47
  • did you try using {% url 'export/' %} in the template. The main issue is that the urls are not matching. In the error msg it doesn't show trying to match after /emarshalapp/ – Swakeert Jain Aug 30 '16 at 14:53
  • Please show your `INSTALLED_APPS` setting and full `AttorneyAdmin` class. I don't think that `get_url` in your `urls.py` is doing anything (otherwise it would throw an error) so remove it. I would also change the name to `name='export'` without a slash. – Alasdair Aug 30 '16 at 15:41
  • @SwakeertJain I can try that - is there a specific place I would put {% url 'export/' %} within the template? – Nicole Marie Aug 30 '16 at 15:42
  • @Alasdair updated my post – Nicole Marie Aug 30 '16 at 15:54
  • I think you might be hitting a problem in django-simple-history (for example see [this question](http://stackoverflow.com/questions/37456942/django-error-accessing-admin-auth-user-history-using-django-simple-history-modu) and [this issue](https://github.com/treyhunner/django-simple-history/issues/222)). I'm not familiar with the package, so I'm not sure what the solution is. – Alasdair Aug 30 '16 at 16:08
  • @Alasdair I added a link to the full traceback I get, in case that sheds some light. From the traceback it seems like a URLconf error of some kind? but I am not sure. – Nicole Marie Aug 30 '16 at 18:16

0 Answers0