3

I'd like to pass an argument {{x}}to my custom file change_form.html, which is located in /home/django/project/app/template/admin/change_form.html. I found this code but it doesn't work:

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)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
paveu
  • 31
  • 1
  • 2

1 Answers1

2

I think I can actually answer this question (for anyone else that find this question through google).

Django 1.4 actually changed the way change_view is defined and some documentation or snippets you might find on the internet have not yet been updated.

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

In other words, this should work:

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)
TheThing
  • 160
  • 1
  • 11