3

我想将一个参数传递{{x}}给我的自定义文件 change_form.html,该文件位于/home/django/project/app/template/admin/change_form.html. 我找到了这段代码,但它不起作用:

class MyModelAdmin(admin.ModelAdmin):
    # A template for a very customized change view:
    change_form_template = 'admin/change_form.html'

    def get_osm_info(self):
        z = Klass()
        x = z.final_down()
        return x

    def change_view(self, request, object_id, extra_context=None):
        my_context = { 'x': get_osm_info(),}
        return super(MyModelAdmin, self).change_view(request, object_id,extra_context=my_context)
4

1 回答 1

2

我想我实际上可以回答这个问题(对于通过谷歌找到这个问题的其他人)。

Django 1.4 实际上改变了 change_view 的定义方式,您可能在 Internet 上找到的一些文档或片段尚未更新。

https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.change_view

换句话说,这应该有效:

class MyModelAdmin(admin.ModelAdmin):
    # A template for a very customized change view:
    change_form_template = 'admin/change_form.html'

    def get_osm_info(self):
        z = Klass()
        x = z.final_down()
        return x

    def change_view(self, request, object_id, form_url='', extra_context=None):
        context = {}
        context.update(extra_context or {})
        context.update({ 'x': get_osm_info(),})
        return super(MyModelAdmin, self).change_view(request, object_id, form_url, context)
于 2012-10-24T16:36:54.330 回答