如何将自定义 HTML 放入 django 管理的更改视图中?

发布于 2024-09-30 03:03:03 字数 338 浏览 0 评论 0原文

当我描述模型的管理选项时,可以将任何方法放入“list_display”中,然后我们可以添加“allow_tags=True”并获取 HTML。除了“changeview”表单之外,还有其他相同的选项吗? 我可以更改表单的模板,但有一些计算会返回 HTML,因此将它们全部放入模板中并不是一个好主意。

class OrderAdmin(admin.ModelAdmin):
    list_display = ('__unicode__','render_html')

render_html 是 model 的一个方法,它在 listview 中有效,将其放入“fields”中不起作用。

When I describe admin options for a model it's possible to put any method in 'list_display' then we can add 'allow_tags=True' and get HTML. Is there any same options but for 'changeview' form?
I can change template for the form but there is some calculation which returns HTML, so It's not good idea to put them all to a template.

class OrderAdmin(admin.ModelAdmin):
    list_display = ('__unicode__','render_html')

render_html is a method of model, it works in listview, Putting it to 'fields' doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮倦 2024-10-07 03:03:03

这是一个 hack,但它应该可以工作。

 class OrderAdmin(admin.ModelAdmin):        
     def render_change_form(self, request, context, *args, **kwargs):
         context['adminform'].form.fields['somefield'].helptext= self.colored_name()
         return super(OrderAdmin, self).render_change_form(request, context, args, kwargs) 
     def colored_name(self):
         return '<span style="color: #%s;">%s %s</span>' % (self.color_code, self.first_name, self.last_name)
     colored_name.allow_tags = True  

This is a hack but it should work.

 class OrderAdmin(admin.ModelAdmin):        
     def render_change_form(self, request, context, *args, **kwargs):
         context['adminform'].form.fields['somefield'].helptext= self.colored_name()
         return super(OrderAdmin, self).render_change_form(request, context, args, kwargs) 
     def colored_name(self):
         return '<span style="color: #%s;">%s %s</span>' % (self.color_code, self.first_name, self.last_name)
     colored_name.allow_tags = True  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文