Code

Ticket #6903: 6903.r16617.diff

File 6903.r16617.diff, 2.6 KB (added by graveyboat, 3 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.

Line 
1Index: django/contrib/admin/options.py
2===================================================================
3--- django/contrib/admin/options.py     (revision 16617)
4+++ django/contrib/admin/options.py     (working copy)
5@@ -813,7 +820,9 @@
6             # Figure out where to redirect. If the user has change permission,
7             # redirect to the change-list page for this object. Otherwise,
8             # redirect to the admin index.
9-            if self.has_change_permission(request, None):
10+            if self.has_change_permission(request, None) and 'HTTP_REFERER' in request.POST:
11+                return HttpResponseRedirect(request.POST['HTTP_REFERER'])
12+            elif self.has_change_permission(request, None):
13                 return HttpResponseRedirect('../')
14             else:
15                 return HttpResponseRedirect('../../../')
16@@ -979,6 +988,7 @@
17         "The 'change' admin view for this model."
18         model = self.model
19         opts = model._meta
20+        HTTP_REFERER = None
21 
22         obj = self.get_object(request, unquote(object_id))
23 
24@@ -1033,6 +1043,9 @@
25                                   queryset=inline.queryset(request))
26                 formsets.append(formset)
27 
28+            if 'HTTP_REFERER' in request.META:
29+                HTTP_REFERER = request.META['HTTP_REFERER']
30+
31         adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj),
32             self.get_prepopulated_fields(request, obj),
33             self.get_readonly_fields(request, obj),
34@@ -1059,6 +1072,7 @@
35             'inline_admin_formsets': inline_admin_formsets,
36             'errors': helpers.AdminErrorList(form, formsets),
37             'app_label': opts.app_label,
38+            'HTTP_REFERER': HTTP_REFERER,
39         }
40         context.update(extra_context or {})
41         return self.render_change_form(request, context, change=True, obj=obj)
42Index: django/contrib/admin/templates/admin/change_form.html
43===================================================================
44--- django/contrib/admin/templates/admin/change_form.html       (revision 16617)
45+++ django/contrib/admin/templates/admin/change_form.html       (working copy)
46@@ -37,6 +37,7 @@
47 <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 %}
48 <div>
49 {% if is_popup %}<input type="hidden" name="_popup" value="1" />{% endif %}
50+{% if HTTP_REFERER %}<input type="hidden" name="HTTP_REFERER" value="{{ HTTP_REFERER }}" />{% endif %}
51 {% if save_on_top %}{% submit_row %}{% endif %}
52 {% if errors %}
53     <p class="errornote">