我正在尝试将导出按钮合并到我的Django管理站点中。能够在管理员中显示按钮,但每次单击按钮时它都会中断,我收到以下错误:
NoReverseMatch at / admin / emarshalapp / attorney / export / Reverse for ' app_list'参数'()'和关键字参数' {' app_label': ''}'未找到。尝试过1种模式: ['管理/(?Pfiler | emarshalapp | AUTH)/ $']
如果有帮助,这里是the full traceback。
我正在尝试按照this tutorial来执行导出逻辑,但我显然必须做错事,只是不确定是什么。我已经在SO(包括this answer)和其他地方尝试过针对NoReverseMatch推荐的所有解决方案,但是看不到任何修复。我很难过,请帮忙!
添加按钮的change-list.html
模板部分:
{% block object-tools-items %}
{{ block.super }}
<li>
<a href="export/"
class="grp-state-focus addlink">Export</a>
</li>
{% endblock %}
这是我的INSTALLED_APPS
设置:
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'))
然后根据教程我有一个excel_utils.py
:
def WriteToExcel(attorney_range, attorney=None):
output = StringIO.StringIO()
workbook = xlsxwriter.Workbook(output)
worksheet_s = workbook.add_worksheet("Summary")
[code to add excel data]
我的律师管理员班级:
@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 = (...)