[Answer]-How can i change the Django admin template on the fly

1๐Ÿ‘

โœ…

I think you need to override the changelist_view and act on the TemplateResponse() returned from it or change the variable holding that name just before that call.

The original function is like this

def changelist_view(self, request, extra_context=None):
    # a lot of stuff happen here
    return TemplateResponse(request, self.change_list_template or [
        'admin/%s/%s/change_list.html' % (app_label, opts.object_name.lower()),
        'admin/%s/change_list.html' % app_label,
        'admin/change_list.html'
    ], context, current_app=self.admin_site.name)

so I think that a code like

def changelist(self, request, extra_context=None):
    if request.GET['foo']:
        self.change_list_template = "students/student_change_list_other.html"

    return super(STUDENTAdmin, self).changelist_view(request, extra_context)
๐Ÿ‘คgipi

Leave a comment