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.
我想为我的模型在列表视图的“添加”按钮旁边添加一个按钮,然后创建一个视图函数,我将在这里完成我的工作,然后将用户重定向回列表视图。
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.
我已经检查了如何重载管理模板,但是我仍然不知道,我应该把我的视图函数放在哪里,我将在哪里完成我的工作,以及我如何将这个视图注册到管理url中。
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.
还有关于安全的问题。我希望在admin中有这个操作,所以如果u r没有登录,u就不能使用它。
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/
我发现了这个,但是我不知道它是否正确:http://www.stavros.io/posts/how- extend- django-admin-site-with-custom/
3 个解决方案
#1
38
When several applications provide different versions of the same resource (template, static file, management command, translation), the application listed first in INSTALLED_APPS has precedence. - Django documentation on INSTALLED_APPS
当几个应用程序提供相同资源的不同版本(模板、静态文件、管理命令、翻译)时,INSTALLED_APPS中首先列出的应用程序具有优先级。- Django关于INSTALLED_APPS的文档
Make sure your app is listed before 'django.contrib.admin'
in INSTALLED_APPS
.
确保你的应用在“django.managb”之前列出。在INSTALLED_APPS管理”。
Create a change_list.html
template in one of the following directories:
创建一个change_list。html模板在下列目录之一:
# 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"
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:
您可以查找原始change_list的内容。它位于path/to/您的/站点包/django/悔过书/admin/ admin/模板/admin/change_list.html中。另一个答案还向您展示了如何格式化模板。Nikolai Saiko向您展示了如何使用“extend”和“super”来覆盖相关部分。简介:
{% extends "admin/change_list.html" %} {% load i18n %}
{% block object-tools-items %}
{{ block.super }}
<li>
<a class="historylink" href="...">My custom admin page</a>
</li>
{% endblock %}
Let's fill href="..."
with an url. The admin url names are in the namespace 'admin' and can be looked up like this:
让我们用url填充href="…"。管理url名称在名称空间“admin”中,可以这样查找:
{% url 'admin:custom_view' %}
When you are adding a button to change_form.html you maybe want to pass in the current object id:
当您向change_form添加一个按钮时。您可能想要传递当前对象id:
{% url 'admin:custom_view' original.pk %}
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:
现在创建一个自定义视图。这可以是一个常规视图(就像您网站上的其他页面一样),也可以是admin.py中的一个自定义管理视图。ModelAdmin上的get_urls方法以与URLconf相同的方式返回要用于该ModelAdmin的url。因此,您可以将它们扩展到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"
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
请阅读有关如何在ModelAdmin中设置视图的权限的文档:https://docs.djangoproject.com/en/1.5/ref/admin/admin/ # django.. 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):
...
You might also want to check request.user.is_active
and handle inactive users.
您可能还想检查request.user。is_active并处理不活跃的用户。
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/
更新:尽可能少地利用框架和定制。很多时候,动作是一个很好的选择:https://docs.djangoproject.com/en/1.5/ ref/悔过/admin/actions/
Update 2: I removed a JS example to inject a button client side. If you need it, see the revisions.
更新2:我删除了一个JS示例来注入一个按钮客户端。如果你需要它,看看修改。
#2
12
Here is another solution , without using of jQuery (like one provided by allcaps). Also this solution provides object's pk with more intuitive way :)
下面是另一种解决方案,不使用jQuery(如allcaps提供的解决方案)。这个解决方案也为对象的pk提供了更直观的方式:)
I'll give my source code based on that link (follow link above for more info):
我将基于那个链接给出我的源代码(更多信息请参见上面的链接):
I have an app Products with model Product. This code adds button "Do Evil", which executes ProductAdmin.do_evil_view()
我有一个应用产品和模型产品。这段代码添加了按钮“Do Evil”,它执行ProductAdmin.do_evil_view()
File products/models.py:
文件的产品/ models.py:
class ProductAdmin(admin.ModelAdmin):
def get_urls(self):
urls = super().get_urls()
my_urls = patterns('',
(r'^(?P<pk>\d+)/evilUrl/$', self.admin_site.admin_view(self.do_evil_view))
)
return my_urls + urls
def do_evil_view(self, request, pk):
print('doing evil with', Product.objects.get(pk=int(pk)))
return redirect('/admin/products/product/%s/' % pk)
self.admin_site.admin_view is needed to ensure that user was logged as administrator.
self.admin_site。需要admin_view以确保用户被记录为管理员。
And this is template extention of standard Django admin page for changing entry:
File: {template_dir}/admin/products/product/change_form.html
这是标准Django管理页面的模板扩展,用于更改条目:File: {template_dir}/admin/products/product/change_form.html
In Django >= 1.8 (thanks to @jenniwren for this info):
在Django >= 1.8中(感谢@jenniwren提供此信息):
{% extends "admin/change_form.html" %}
{% load i18n %}
{% block object-tools-items %}
{{ block.super }}
<li><a class="historylink" href="evilUrl/">{% trans "Do Evil" %}</a></li>
{% endblock %}
If your Django version is lesser than 1.8, you have to write some more code:
如果Django版本小于1.8,则需要编写更多代码:
{% extends "admin/change_form.html" %}
{% load i18n %}
{% block object-tools %}
{% if change %}{% if not is_popup %}
<ul class="object-tools">
<li><a class="historylink" href="history/">{% trans "History" %}</a></li>
<li><a class="historylink" href="evilUrl/">{% trans "Do Evil" %}</a></li>
{% if has_absolute_url %}
<li><a class="viewsitelink" href="../../../r/{{ content_type_id }}/{{ object_id }}/">{% trans "View on site" %}</a></li>
{% endif%}</ul>
{% endif %}{% endif %}
{% endblock %}
#3
0
This package helps adding custom views with navigation buttons to django admin:
这个包有助于为django admin添加带有导航按钮的自定义视图:
https://github.com/saxix/django-admin-extra-urls
https://github.com/saxix/django-admin-extra-urls
#1
38
When several applications provide different versions of the same resource (template, static file, management command, translation), the application listed first in INSTALLED_APPS has precedence. - Django documentation on INSTALLED_APPS
当几个应用程序提供相同资源的不同版本(模板、静态文件、管理命令、翻译)时,INSTALLED_APPS中首先列出的应用程序具有优先级。- Django关于INSTALLED_APPS的文档
Make sure your app is listed before 'django.contrib.admin'
in INSTALLED_APPS
.
确保你的应用在“django.managb”之前列出。在INSTALLED_APPS管理”。
Create a change_list.html
template in one of the following directories:
创建一个change_list。html模板在下列目录之一:
# 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"
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:
您可以查找原始change_list的内容。它位于path/to/您的/站点包/django/悔过书/admin/ admin/模板/admin/change_list.html中。另一个答案还向您展示了如何格式化模板。Nikolai Saiko向您展示了如何使用“extend”和“super”来覆盖相关部分。简介:
{% extends "admin/change_list.html" %} {% load i18n %}
{% block object-tools-items %}
{{ block.super }}
<li>
<a class="historylink" href="...">My custom admin page</a>
</li>
{% endblock %}
Let's fill href="..."
with an url. The admin url names are in the namespace 'admin' and can be looked up like this:
让我们用url填充href="…"。管理url名称在名称空间“admin”中,可以这样查找:
{% url 'admin:custom_view' %}
When you are adding a button to change_form.html you maybe want to pass in the current object id:
当您向change_form添加一个按钮时。您可能想要传递当前对象id:
{% url 'admin:custom_view' original.pk %}
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:
现在创建一个自定义视图。这可以是一个常规视图(就像您网站上的其他页面一样),也可以是admin.py中的一个自定义管理视图。ModelAdmin上的get_urls方法以与URLconf相同的方式返回要用于该ModelAdmin的url。因此,您可以将它们扩展到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"
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
请阅读有关如何在ModelAdmin中设置视图的权限的文档:https://docs.djangoproject.com/en/1.5/ref/admin/admin/ # django.. 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):
...
You might also want to check request.user.is_active
and handle inactive users.
您可能还想检查request.user。is_active并处理不活跃的用户。
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/
更新:尽可能少地利用框架和定制。很多时候,动作是一个很好的选择:https://docs.djangoproject.com/en/1.5/ ref/悔过/admin/actions/
Update 2: I removed a JS example to inject a button client side. If you need it, see the revisions.
更新2:我删除了一个JS示例来注入一个按钮客户端。如果你需要它,看看修改。
#2
12
Here is another solution , without using of jQuery (like one provided by allcaps). Also this solution provides object's pk with more intuitive way :)
下面是另一种解决方案,不使用jQuery(如allcaps提供的解决方案)。这个解决方案也为对象的pk提供了更直观的方式:)
I'll give my source code based on that link (follow link above for more info):
我将基于那个链接给出我的源代码(更多信息请参见上面的链接):
I have an app Products with model Product. This code adds button "Do Evil", which executes ProductAdmin.do_evil_view()
我有一个应用产品和模型产品。这段代码添加了按钮“Do Evil”,它执行ProductAdmin.do_evil_view()
File products/models.py:
文件的产品/ models.py:
class ProductAdmin(admin.ModelAdmin):
def get_urls(self):
urls = super().get_urls()
my_urls = patterns('',
(r'^(?P<pk>\d+)/evilUrl/$', self.admin_site.admin_view(self.do_evil_view))
)
return my_urls + urls
def do_evil_view(self, request, pk):
print('doing evil with', Product.objects.get(pk=int(pk)))
return redirect('/admin/products/product/%s/' % pk)
self.admin_site.admin_view is needed to ensure that user was logged as administrator.
self.admin_site。需要admin_view以确保用户被记录为管理员。
And this is template extention of standard Django admin page for changing entry:
File: {template_dir}/admin/products/product/change_form.html
这是标准Django管理页面的模板扩展,用于更改条目:File: {template_dir}/admin/products/product/change_form.html
In Django >= 1.8 (thanks to @jenniwren for this info):
在Django >= 1.8中(感谢@jenniwren提供此信息):
{% extends "admin/change_form.html" %}
{% load i18n %}
{% block object-tools-items %}
{{ block.super }}
<li><a class="historylink" href="evilUrl/">{% trans "Do Evil" %}</a></li>
{% endblock %}
If your Django version is lesser than 1.8, you have to write some more code:
如果Django版本小于1.8,则需要编写更多代码:
{% extends "admin/change_form.html" %}
{% load i18n %}
{% block object-tools %}
{% if change %}{% if not is_popup %}
<ul class="object-tools">
<li><a class="historylink" href="history/">{% trans "History" %}</a></li>
<li><a class="historylink" href="evilUrl/">{% trans "Do Evil" %}</a></li>
{% if has_absolute_url %}
<li><a class="viewsitelink" href="../../../r/{{ content_type_id }}/{{ object_id }}/">{% trans "View on site" %}</a></li>
{% endif%}</ul>
{% endif %}{% endif %}
{% endblock %}
#3
0
This package helps adding custom views with navigation buttons to django admin:
这个包有助于为django admin添加带有导航按钮的自定义视图:
https://github.com/saxix/django-admin-extra-urls
https://github.com/saxix/django-admin-extra-urls