Django如何将自定义变量传递给上下文以在自定义管理模板中使用?

时间:2012-02-09 22:41:10

标签: django templates admin extends django-context

我正在扩展change_list.html,我需要输出settings.py

中定义的变量

如何将该特定变量传递到自定义管理模板上下文?

1 个答案:

答案 0 :(得分:68)

class MyModelAdmin(admin.ModelAdmin):
    ...
    def changelist_view(self, request, extra_context=None):
        extra_context = extra_context or {}
        extra_context['some_var'] = 'This is what I want to show'
        return super(MyModelAdmin, self).changelist_view(request, extra_context=extra_context)

请参阅:https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.changelist_view