django change_form.html

时间:2010-10-23 12:00:22

标签: django django-context

我想将参数{{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)

1 个答案:

答案 0 :(得分:2)

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

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

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)