Skip to content Skip to sidebar Skip to footer

Can't Figure Out Why I'm Getting `reverse For 'app_list' With Keyword Arguments '{'app_label': ''}' Not Found` In A Django Project

The project has 2 apps - accounts (very basic custom users) and portal. myproject/myproject/urls.py: from django.contrib import admin from django import urls from portal import adm

Solution 1:

First, you are using a view outside of the admin, which is fine, but you are using an admin template change_list.html which assumes some variables sent out with the template context. I would suggest to add custom urls to your admin.py (see bellow), and send the necessary context so the template is rendered properly.

I had the same issue, which was caused in this line:

<li><a href="{% url 'admin:app_list' app_label=opts.app_label %}">{{ opts.app_config.verbose_name }}</a></li>

When looking on the context provided to the template with django-debug-toolbar I noticed that the opts variable was not being sent. In your case, the culprit is probably cl.

So I had to update my custom urls code and sent it myself:

defget_urls(self):
        urls = super().get_urls()
        opts = self.model._meta
        custom_urls = [
            url(
                r'^(?P<some_id>.+)/actions/task_start/$',
                self.admin_site.admin_view(SomeCustomFormView.as_view(
                    extra_context={'opts': opts}
                )),
                name='task_start',
            ),
        ]
        return custom_urls + urls

The cl variable is got from the request: cl = self.get_changelist_instance(request) and sent out with the context like this (django/contrib/admin/options.py):

context = {
            **self.admin_site.each_context(request),
            'module_name': str(opts.verbose_name_plural),
            'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)},
            'selection_note_all': selection_note_all % {'total_count': cl.result_count},
            'title': cl.title,
            'subtitle': None,
            'is_popup': cl.is_popup,
            'to_field': cl.to_field,
            'cl': cl,
            'media': media,
            'has_add_permission': self.has_add_permission(request),
            'opts': cl.opts,
            'action_form': action_form,
            'actions_on_top': self.actions_on_top,
            'actions_on_bottom': self.actions_on_bottom,
            'actions_selection_counter': self.actions_selection_counter,
            'preserved_filters': self.get_preserved_filters(request),
            **(extra_context or {}),
        }

But you don't have easy access to the request in get_urls so I would change the template code and instead of using cl I would use opts which I get from the model meta field.

Post a Comment for "Can't Figure Out Why I'm Getting `reverse For 'app_list' With Keyword Arguments '{'app_label': ''}' Not Found` In A Django Project"