Django 管理模型 add_view:如何删除“保存并添加另一个”按钮?
Posted
技术标签:
【中文标题】Django 管理模型 add_view:如何删除“保存并添加另一个”按钮?【英文标题】:Django admin model add_view: how to remove "save and add another" buttons? 【发布时间】:2014-11-18 10:45:03 【问题描述】:通过实现以下代码,我能够删除“保存并添加另一个”和“保存并继续编辑”按钮:
# At the start of my admin.py file I have:
from django.contrib.admin.templatetags.admin_modify import *
from django.contrib.admin.templatetags.admin_modify import submit_row as original_submit_row
@register.inclusion_tag('admin/submit_line.html', takes_context=True)
def submit_row(context):
ctx = original_submit_row(context)
ctx.update(
'show_save_and_add_another': context.get('show_save_and_add_another', ctx['show_save_and_add_another']),
'show_save_and_continue': context.get('show_save_and_continue', ctx['show_save_and_continue'])
)
return ctx
class MyModelAdmin(GuardedModelAdmin):
# Then inside MyModelAdmin I have this:
def change_view(self, request, object_id, form_url='', extra_context=None):
extra_context = extra_context or
extra_context['show_save_and_add_another'] = False
extra_context['show_save_and_continue'] = False
return super(MyModelAdmin, self).change_view(request, object_id,
form_url, extra_context=extra_context)
这在我使用 change_view 时效果很好,但是当我添加模型的新实例时,按钮会重新出现。我尝试了以下方法:
def add_view(self, request, form_url='', extra_context=None):
extra_context = extra_context or
extra_context['show_save_and_add_another'] = False
extra_context['show_save_and_continue'] = False
return super(MyModelAdmin, self).add_view(self, request, form_url='', extra_context=extra_context)
但它给了我一个奇怪的 MissingAtrribute 错误——这是回溯:
Traceback:
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/contrib/admin/options.py" in wrapper
432. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/utils/decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
52. response = view_func(request, *args, **kwargs)
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/contrib/admin/sites.py" in inner
198. return view(request, *args, **kwargs)
File "/home/username/Development/MyProject/webapp/MyModel/admin.py" in add_view
153. return super(MyModelAdmin, self).add_view(self, request, form_url='', extra_context=extra_context)
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/utils/decorators.py" in _wrapper
29. return bound_func(*args, **kwargs)
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/utils/decorators.py" in _wrapped_view
95. result = middleware.process_view(request, view_func, args, kwargs)
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/middleware/csrf.py" in process_view
111. request.COOKIES[settings.CSRF_COOKIE_NAME])
Exception Type: AttributeError at /admin/MyModel/ModelInstance/add/
Exception Value: 'MyModelAdmin' object has no attribute 'COOKIES'
我正在使用 django-guardian 并想知道这是否会导致我的问题?有谁知道在添加新模型实例时如何从模板的 submit_line 部分摆脱这些烦人的按钮?
【问题讨论】:
【参考方案1】:我有这个代码在一个小项目中工作,我没有放参考,因为我推断它并且它有效:)
##admin.py
class Admincommits(admin.ModelAdmin):
list_display = ["id"]
list_filter = ["id"]
search_fields = ["id"]
actions = None
def add_view(self, request, extra_context=None):
if request.user.is_superuser:
extra_context = extra_context or
extra_context['show_save_and_continue'] = False
#extra_context['show_save_and_add_another'] = False
#extra_context['show_save'] = False
#extra_context['show_delete'] = False
return super(Admincommits, self).add_view(request, extra_context=extra_context)
【讨论】:
【参考方案2】:如果您想为了美观而简单地隐藏这些按钮,您也可以使用 CSS,但这可能不是最好的方法,因为您可以通过检查 css 来启用它们,它当然很简单,而且仍然足够精细,只能隐藏它们在某些模型管理员上。
admin.py:
class MyModelAdmin(admin.ModelAdmin)
....
class Media:
#js = ('' ) # Can include js if needed
css = 'all': ('my_admin/css/my_model.css', )
my_model.css 位于上述路径中的静态文件夹中。
my_model.css:
/* Optionally make the continue and save button look like primary */
input[name="_continue"]
border: 2px solid #5b80b2;
background: #7CA0C7;
color: white;
/* Hide the "Delete", "Add Another" and "Save" buttons, customize this to what you need */
.deletelink, input[name="_addanother"], input[name="_save"]
display: none;
这些按钮的类和名称可能会在 django 版本之间发生变化,我现在使用的是 Django 1.6.6,我认为它们最近没有变化。如果您希望这对您的整个管理站点有效,您可以将 admin/base_site.html 默认模板复制到您的静态目录并覆盖“extrahead”块以包含此样式。见base_site.html。
希望 CSS 方法有所帮助 :) 它肯定不会对您造成任何错误。
【讨论】:
不错的解决方法 -- 如果没有其他人能够追踪错误原因,我会在几天内接受它。以上是关于Django 管理模型 add_view:如何删除“保存并添加另一个”按钮?的主要内容,如果未能解决你的问题,请参考以下文章
当我从数据库/模型中删除对象时,如何让 Django Admin 删除文件?