user2330497
user2330497

Reputation: 419

How can i change the Django admin template on the fly

I have the Django Admin model like this

class STUDENTAdmin(ModelAdmin):
    change_list_template = "students/student_change_list.html"

Now i want to chnage that dynamically based on some request parameter something like

if request.GET['foo']:
        change_list_template = "students/student_change_list_other.html"

how can i do that

Upvotes: 0

Views: 517

Answers (1)

gipi
gipi

Reputation: 2515

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)

Upvotes: 1

Related Questions