2012-02-07 85 views
12

我想在django的提交行中添加一个额外的按钮。 标准我们得到“删除”,“保存”,“保存并继续编辑”和“保存并添加另一个”。为了这个集合,我想添加另一个按钮来调用模型上的一个函数。如何将按钮添加到“submit_row”上下文中django

据我了解模板change_form是在admin.views其中之一产生的。上下文submit_row作为上下文传递。

我想编辑管理视图的上下文。我在哪里可以在我的文件系统中找到它?

+0

当你说“我在哪里可以找到它在我的filesytem?”你的意思是更改表单管理模板? – nabucosound 2012-02-07 17:18:59

+0

不,我是指以submit_row作为其上下文呈现changeform.html的视图 – jorrebor 2012-02-09 17:52:32

+0

您是否解决了这个问题?如果是这样,可否请您发布您的解决方案。 – AgDude 2012-03-28 11:40:06

回答

12

从我可以告诉,有两个相关的文件。首先是

.../django/contrib/admin/templatetags/admin_modify.py 

它具有以下部分:

@register.inclusion_tag('admin/submit_line.html', takes_context=True) 
def submit_row(context): 
    """ 
    Displays the row of buttons for delete and save. 
    """ 
    opts = context['opts'] 
    change = context['change'] 
    is_popup = context['is_popup'] 
    save_as = context['save_as'] 
    return { 
     'onclick_attrib': (opts.get_ordered_objects() and change 
          and 'onclick="submitOrderForm();"' or ''), 
     'show_delete_link': (not is_popup and context['has_delete_permission'] 
           and (change or context['show_delete'])), 
     'show_save_as_new': not is_popup and change and save_as, 
     'show_save_and_add_another': context['has_add_permission'] and 
          not is_popup and (not save_as or context['add']), 
     'show_save_and_continue': not is_popup and context['has_change_permission'], 
     'is_popup': is_popup, 
     'show_save': True 
    } 

而第二个是

.../django/contrib/admin/templates/admin/submit_line.html 

是以下几点:

{% load i18n %} 
<div class="submit-row"> 
{% if show_save %}<input type="submit" value="{% trans 'Save' %}" class="default" name="_save" {{ onclick_attrib }}/>{% endif %} 
{% if show_delete_link %}<p class="deletelink-box"><a href="delete/" class="deletelink">{% trans "Delete" %}</a></p>{% endif %} 
{% if show_save_as_new %}<input type="submit" value="{% trans 'Save as new' %}" name="_saveasnew" {{ onclick_attrib }}/>{%endif%} 
{% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother" {{ onclick_attrib }} />{% endif %} 
{% if show_save_and_continue %}<input type="submit" value="{% trans 'Save and continue editing' %}" name="_continue" {{ onclick_attrib }}/>{% endif %} 
</div> 

你可能只是第二个添加一些自定义的html文件来显示新的按钮。