[Fixed]-Customizing django admin ChangeForm template / adding custom content

23👍

Add your extra context in change_view

class MyObjectAdmin(admin.ModelAdmin):

    # A template for a very customized change view:
    change_form_template = 'admin/my_change_form.html'

    def get_dynamic_info(self):
        # ...
        pass

    def change_view(self, request, object_id, form_url='', extra_context=None):
        extra_context = extra_context or {}
        extra_context['osm_data'] = self.get_dynamic_info()
        return super(MyObjectAdmin, self).change_view(
            request, object_id, form_url, extra_context=extra_context,
        )

15👍

I believe the magic variable you seek is ‘original’, this contains the python object the change form is editing:

<a href="http://example.com/abc/{{ original.id }}?"/>View Website</a>

Leave a comment