如何在django管理员更改列表视图页面中添加一个按钮 [英] How can I add a button into django admin change list view page
问题描述
我想在模型中的列表视图中的添加按钮旁边添加一个按钮,然后创建一个视图函数,我将在此处执行我的内容,然后将用户重定向到列表视图。
I would like to add a button next to "add" button in list view in model for my model and then create a view function where I will do my stuff and then redirect user back to list view.
我已经检查过如何重载管理模板,但是我仍然不知道,我应该把我的视图功能放在哪里,我该怎么做,我该怎么把这个视图注册到管理URL 。
I've checked how to overload admin template, but I still dont know, where should I put my view function where I will do my stuff, and how can I register that view into admin urls.
还有关于安全性的问题。我想在管理员内部执行该操作,所以如果你没有登录,你不能使用它。
There is also question about security. I would like to have that action inside admin, so if u r not logged in, u cannot use it.
我发现这个,但我不知道这是正确的方法: http:/ /www.stavros.io/posts/how-to-extend-the-django-admin-site-with-custom/
I've found this, but I don't know if it's the right way: http://www.stavros.io/posts/how-to-extend-the-django-admin-site-with-custom/
推荐答案
当多个应用程序提供不同版本的相同
资源(模板,静态文件,管理命令,翻译)时,INSTALLED_APPS中首先列出的
应用程序优先。
- 有关INSTALLED_APPS的Django文档
确保您的应用程序在'django.contrib.admin'
之前列出在 INSTALLED_APPS
。
Make sure your app is listed before 'django.contrib.admin'
in INSTALLED_APPS
.
创建一个 change_list.html
模板在以下目录之一:
Create a change_list.html
template in one of the following directories:
# Template applies to all change lists.
myproject/myapp/templates/admin/change_list.html
# Template applies to change lists in myapp.
myproject/myapp/templates/admin/myapp/change_list.html
# Template applies to change list in myapp and only to the Foo model.
myproject/myapp/templates/admin/myapp/foo/change_list.html
模板应该自动拾取,但是如果不在上面列出的路径之一,您还可以通过管理模型属性指向:
The template should be picked up automatically, but in case it is not on one of paths listed above, you can also point to it via an admin model attribute:
class MyModelAdmin(admin.ModelAdmin):
#...
change_list_template = "path/to/change_list.html"
您可以在 path / to / your / site-packages / django中查找原始的change_list.html的内容/contrib/admin/templates/admin/change_list.html
。 其他答案还显示了如何格式化模板。 Nikolai Saiko向您展示如何使用'extend'和'super'覆盖相关的部分。摘要:
You can lookup the contents of the original change_list.html it lives in path/to/your/site-packages/django/contrib/admin/templates/admin/change_list.html
. The other answer also shows you how to format the template. Nikolai Saiko shows you how to override the relevant parts using 'extends' and 'super'. Summary:
{% extends "admin/change_form.html" %} {% load i18n %}
{% block object-tools-items %}
{{ block.super }}
<li>
<a class="historylink" href="...">My custom admin page</a>
</li>
{% endblock %}
我们填写 href =。 ..
与网址。管理员的URL名称在命名空间admin中,可以这样查找:
Let's fill href="..."
with an url. The admin url names are in the namespace 'admin' and can be looked up like this:
{% url 'admin:custom_view' %}
当您添加一个按钮到change_form.html时,您可能想要传入当前对象ID:
When you are adding a button to change_form.html you maybe want to pass in the current object id:
{% url 'admin:custom_view' original.pk %}
现在创建一个自定义视图。这可以是常规视图(就像您网站上的其他页面)或admin.py中的自定义管理视图。 ModelAdmin上的get_urls方法以与URLconf相同的方式返回要用于该ModelAdmin的URL。因此,您可以按照URL调度程序的说明扩展它们:
Now create a custom view. This can be a regular view (just like other pages on your website) or a custom admin view in admin.py. The get_urls method on a ModelAdmin returns the URLs to be used for that ModelAdmin in the same way as a URLconf. Therefore you can extend them as documented in URL dispatcher:
class MyModelAdmin(admin.ModelAdmin):
def get_urls(self):
urls = super(MyModelAdmin, self).get_urls()
my_urls = patterns('',
url(r'^my_view/$', self.my_view, name="custom_view")
)
return my_urls + urls
def my_view(self, request):
# custom view which should return an HttpResponse
pass
# In case your template resides in a non-standard location
change_list_template = "path/to/change_list.html"
阅读有关如何在ModelAdmin中设置视图的权限的文档: https://docs.djangoproject.com/en/1.5/ref/contrib/admin/#django.contrib.admin.ModelAdmin。 get_urls
Read the docs on how to set permissions on a view in ModelAdmin: https://docs.djangoproject.com/en/1.5/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_urls
你可以保护查看您的视图,并且只允许访问具有员工身份的用户:
You can protect your view and only give access to users with staff status:
from django.contrib.admin.views.decorators import staff_member_required
@staff_member_required
def my_view(request):
...
您可能还需要检查 request.user.is_active
并处理非活动用户。
You might also want to check request.user.is_active
and handle inactive users.
更新:尽可能少地利用框架和自定义。很多时候,行动可以是一个很好的选择: https:// docs。 djangoproject.com/en/1.5/ref/contrib/admin/actions/
Update: Take advantage of the framework and customise as little as possible. Many times actions can be a good alternative: https://docs.djangoproject.com/en/1.5/ref/contrib/admin/actions/
更新2:我删除了一个JS示例来注入一个按钮客户端。如果需要,请参阅修订版本。
Update 2: I removed a JS example to inject a button client side. If you need it, see the revisions.
这篇关于如何在django管理员更改列表视图页面中添加一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!