Django管理模型add_view:如何删除"保存并添加另一个"纽扣?

时间:2014-09-24 04:16:57

标签: django django-templates django-admin django-1.6 django-guardian

我可以通过实现以下代码删除“保存并添加另一个”和“保存并继续编辑”按钮:

# 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部分摆脱这些恼人的按钮?

2 个答案:

答案 0 :(得分:4)

如果你想明白地隐藏这些按钮用于装饰目的,你也可以使用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默认模板复制到静态目录中并覆盖“额外”'阻止包括这种风格。请参阅base_site.html

希望CSS方法有所帮助:)它肯定不会给你造成任何错误。

答案 1 :(得分: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)