Django Admin change_form.html에 버튼 추가 및 저장시 custom 작업 수행

2021. 2. 12. 15:45파이썬/Django

 

{% block submit_buttons_bottom %}
{{ block.super }}
<div class="submit-row">
    <input type="submit" value="Save & Go to Boxes" name="_gotoTrainedBoxes">
    <input type="submit" value="Save(include Image) & Go to Boxes" name="_gotoTrainedBoxes(include Image)">
</div>
{% endblock %} 
    def response_change(self, request, obj):
        if "_gotoTrainedBoxes" in request.POST or "_gotoTrainedBoxes(include Image)" in request.POST :
            if "_gotoTrainedBoxes(include Image)" in request.POST :
                Trained_Image.createImageData(obj)

            opts = self.model._meta
            msg = format_html('The Trained_Image “{}” was changed successfully.', obj.id)
            self.message_user(request, msg, messages.SUCCESS)
            redirect_url = reverse('admin:%s_%s_changelist' %
                                   (opts.app_label, 'trained_box'),
                                   #args=(obj.id,),
                                   current_app=self.admin_site.name)
            #redirect_url = add_preserved_filters({'preserved_filters': preserved_filters, 'opts': opts}, redirect_url)
            return HttpResponseRedirect(redirect_url)
        return super(Trained_ImageAdmin, self).response_change(request, obj)