customizing django admin ChangeForm template / adding custom content

后端 未结 2 1535
难免孤独
难免孤独 2021-02-02 00:04

I\'m able to insert (lame) static text onto the change form admin page, but I\'d really like it to use the context of the current object being edited!

For instance, I w

相关标签:
2条回答
  • 2021-02-02 00:33

    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,
        )
    
    0 讨论(0)
  • 2021-02-02 00:42

    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>
    
    0 讨论(0)
提交回复
热议问题