Ticket #6903: 6903.r16617.diff

File 6903.r16617.diff, 2.6 KB (added by graveyboat, 4 years ago)

Proposed patch. This stores the HTTP_REFERER as a hidden input in the form (if REQUEST.METAHTTP_REFERER? exists). On change, the user is redirected back from whence they came.

  • django/contrib/admin/options.py

     
    813820            # Figure out where to redirect. If the user has change permission,
    814821            # redirect to the change-list page for this object. Otherwise,
    815822            # redirect to the admin index.
    816             if self.has_change_permission(request, None):
     823            if self.has_change_permission(request, None) and 'HTTP_REFERER' in request.POST:
     824                return HttpResponseRedirect(request.POST['HTTP_REFERER'])
     825            elif self.has_change_permission(request, None):
    817826                return HttpResponseRedirect('../')
    818827            else:
    819828                return HttpResponseRedirect('../../../')
     
    979988        "The 'change' admin view for this model."
    980989        model = self.model
    981990        opts = model._meta
     991        HTTP_REFERER = None
    982992
    983993        obj = self.get_object(request, unquote(object_id))
    984994
     
    10331043                                  queryset=inline.queryset(request))
    10341044                formsets.append(formset)
    10351045
     1046            if 'HTTP_REFERER' in request.META:
     1047                HTTP_REFERER = request.META['HTTP_REFERER']
     1048
    10361049        adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj),
    10371050            self.get_prepopulated_fields(request, obj),
    10381051            self.get_readonly_fields(request, obj),
     
    10591072            'inline_admin_formsets': inline_admin_formsets,
    10601073            'errors': helpers.AdminErrorList(form, formsets),
    10611074            'app_label': opts.app_label,
     1075            'HTTP_REFERER': HTTP_REFERER,
    10621076        }
    10631077        context.update(extra_context or {})
    10641078        return self.render_change_form(request, context, change=True, obj=obj)
  • django/contrib/admin/templates/admin/change_form.html

     
    3737<form {% if has_file_field %}enctype="multipart/form-data" {% endif %}action="{{ form_url }}" method="post" id="{{ opts.module_name }}_form">{% csrf_token %}{% block form_top %}{% endblock %}
    3838<div>
    3939{% if is_popup %}<input type="hidden" name="_popup" value="1" />{% endif %}
     40{% if HTTP_REFERER %}<input type="hidden" name="HTTP_REFERER" value="{{ HTTP_REFERER }}" />{% endif %}
    4041{% if save_on_top %}{% submit_row %}{% endif %}
    4142{% if errors %}
    4243    <p class="errornote">
Back to Top