在django管理员中为一个对象添加一个额外的按钮 [英] Adding an extra button to one object in django admin

查看:901
本文介绍了在django管理员中为一个对象添加一个额外的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这个没有被问到,我只是错过了,但我搜索了一堆,找不到任何东西。



添加或更改对象时,我向django管理员添加了一个额外的保存按钮。这样做相当容易我只是覆盖submit_line.html来添加额外的按钮,然后覆盖save_model函数来检查该按钮的名称。它的作品很棒



我的问题是,我只需要这个按钮来显示一个特定的对象,而不是全部。我查看了change_form.html,看看它是如何知道它正在处理的对象,并找到了{{opts.module_name}},但在submit_line.html中似乎不可访问。我试着打印出来,没有出现。



我还想到了黑客save_as(不是很优雅,但我真的不在乎这个特定的项目),但是那个按钮只会显示出来。添加,这样就不行了。



有没有人知道如何在submit_line.html中检测到我正在使用的对象?还是其他任何一种方法?



谢谢!

解决方案

你可以使用这样的javascript:



/static/js/useful.js

 
$(document).ready(function($){
$('input [name =_ addanother]')before('< input type =submitname =_ use value =有用功能/>');
});

并在您的ModelAdmin中添加:

 
class MyModelAdmin(admin.ModelAdmin):
class Media:
js =('/static/js/useful.js')


I hope this hasn't been asked and I just missed it, but I searched a bunch and couldn't find anything.

I'm adding an extra save button to the django admin when adding or changing an object. Doing that is fairly easy. I just overrode the submit_line.html to add the extra button and then overrode the save_model function to check for the name of that button. It works great.

My problem is that I only need this button to appear for one particular object... not all of them. I looked in change_form.html to see how it knows what object it is dealing with and found {{ opts.module_name }}, but it doesn't seem to be accessible in submit_line.html. I tried printing it out and nothing showed up.

I also thought about hacking save_as (not very graceful, but I don't really care for this particular project), but that button only shows up on change.. not on add, so that won't work.

Does anyone know how to detect what object I'm working with in submit_line.html? Or any other way of doing this?

Thanks!

解决方案

You can do it using javascript like this:

/static/js/useful.js

$(document).ready(function ($) {
    $('input[name="_addanother"]').before('<input type="submit" name="_use" value="Useful functionality"/>');
});

and in your ModelAdmin add:

class MyModelAdmin(admin.ModelAdmin):
     class Media:
        js = ('/static/js/useful.js',)

这篇关于在django管理员中为一个对象添加一个额外的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆