我在change_form中有另一个提交按钮,如果单击此按钮,我想验证并自定义我请求的数据。
admin.py
def save_model(self, request, obj, form, change):
data = request.GET.copy()
if '_saveandclose' in request.POST:
data['CloseDetail'] = "Save and Closed Clicked!"
obj.User = request.user
obj.save()
答案 0 :(得分:0)
要向管理模板添加新按钮(即admin / change_form.html),您必须override the admin template并添加自定义submit_buttons_bottom
块。模板标签用于呈现默认提交按钮,您可以在此处添加自定义按钮和样式。
{% block submit_buttons_bottom %}{% submit_row %}{% endblock %}
答案 1 :(得分:0)
1.将/your_python_path/python_version/site-packages/lib/django/crontab/admin/template
复制到您的项目中,并将TEMPLATE配置添加到您的设置中
(你也可以在你的venv中编辑它而不复制到你的项目中)
2.然后编辑your_proj/template/admin/submit_line.html
,根据需要添加/修改一些<input>/<button>
e.x。 <button class="default" onclick="xxxx" > sth </button>
答案 2 :(得分:0)
出于文档目的,我创建了@Radishx解决方案的更完整版本。这将在QuestionAdmin
中添加一个发布按钮。
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['backend/templates'], # Search in backend app first to overwrite admin
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
backend / templates / admin / submit_line.html
{% load i18n admin_urls %}
<div class="submit-row">
{% block submit-row %}
{% if show_save %}<input type="submit" value="{% trans 'Save' %}" class="default" name="_save">{% endif %}
{% if show_delete_link %}
{% url opts|admin_urlname:'delete' original.pk|admin_urlquote as delete_url %}
<p class="deletelink-box"><a href="{% add_preserved_filters delete_url %}" class="deletelink">{% trans "Delete" %}</a></p>
{% endif %}
{% if show_save_as_new %}<input type="submit" value="{% trans 'Save as new' %}" name="_saveasnew">{% endif %}
{% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother">{% endif %}
{% if show_save_and_continue %}<input type="submit" value="{% if can_change %}{% trans 'Save and continue editing' %}{% else %}{% trans 'Save and view' %}{% endif %}" name="_continue">{% endif %}
{% if show_publish %}<input type="submit" value="{% trans 'Publish' %}" class="default" name="_publish">{% endif %}
{% if show_close %}<a href="{% url opts|admin_urlname:'changelist' %}" class="closelink">{% trans 'Close' %}</a>{% endif %}
{% endblock %}
</div>
请注意{% if show_publish %}
。
后端/admin.py
@admin.register(Question)
class QuestionAdmin(admin.ModelAdmin):
def change_view(self, request, object_id, form_url='', extra_context=None):
try:
extra_context['show_publish'] = True
except TypeError:
extra_context = {'show_publish': True}
return self.changeform_view(request, object_id, form_url, extra_context)
将backend
更改为您的应用名称。
之后,您必须确保Django知道如何使用_publish按钮。