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 = (...)