如何将Django管理员自定义按钮重定向到视图

时间:2018-12-05 14:34:47

标签: django django-forms django-views

我做了一个Django自定义按钮:

{% extends 'admin/change_form.html' %}
{% block submit_buttons_bottom %}
{{ block.super }}
<div class="submit-row">
<input type="submit" value="Make Unique" name="_make-unique">
</div>
{% endblock %}

然后我要将此按钮链接到视图:

类似这样的东西:

<input type="submit" value="Make Unique" name="_make-unique" onclick="location.href="{% url '/response_change' %}>

这是我的url.py:

urlpatterns = [
url(r'^admin/', admin.site.urls),
path('admin/response_change/', views.response_change, name='response_change'),
]

和view.py:

def response_change(self, request, obj):
    if "_make-unique" in request.POST:
    matching_names_except_this = self.get_queryset(request).filter(name=obj.
    ˓→name).exclude(pk=obj.id)
    matching_names_except_this.delete()
    obj.is_unique = True
    obj.save()
    self.message_user(request, "This villain is now unique")
    return HttpResponseRedirect(".")
return super().response_change(request, obj)

任何想法我该如何解决!!

0 个答案:

没有答案