customizing django admin ChangeForm template / adding custom content(自定义 django admin ChangeForm 模板/添加自定义内容)
问题描述
我能够在更改表单管理页面上插入(蹩脚的)静态文本,但我真的希望它使用正在编辑的当前对象的上下文!
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!
例如,我想在 MyObject 更改表单上格式化 URL,以包含 ForeignKey 连接对象 (obj
) 中的 ID 作为链接.
For instance, I want to format on the MyObject change form a URL to include the ID from a ForeignKey connected object (obj
) as a link.
我的管理对象:
class MyObjectChangeForm(forms.ModelForm):
class Meta:
model = MyObject
fields = ('field1', 'obj',)
class MyObjectAdmin(admin.ModelAdmin):
form = MyObjectChangeForm
list_display = ('field1', 'obj')
def render_change_form(self, request, context, *args, **kwargs):
self.change_form_template = 'admin/my_change_form.html'
extra = {'lame_static_text': "something static",}
context.update(extra)
return super(MyObjectAdmin, self).render_change_form(request,
context, *args, **kwargs)
我的模板templates/admin/my_change_form.html
:
{% extends "admin/change_form.html" %}
{% block form_top %}
{{ lame_static_text }}
<a href="http://example.com/abc/{{ adminform.data.obj.id }}?"/>View Website</a>
{% endblock %}
{{adminform.data.obj.id}}
调用显然不起作用,但我想要一些类似的东西.
The {{adminform.data.obj.id}}
call obviously doesn't work, but I'd like something along those lines.
如何将当前对象的动态上下文插入到管理员更改表单中?
How do I insert dynamic context from the current object into the admin change form?
推荐答案
在 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,
)
这篇关于自定义 django admin ChangeForm 模板/添加自定义内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:自定义 django admin ChangeForm 模板/添加自定义内容
基础教程推荐
- 症状类型错误:无法确定关系的真值 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01