Ticket #6903: return_to_old_change_list.diff

File return_to_old_change_list.diff, 4.8 KB (added by slonopotamus, 7 years ago)
  • django/contrib/admin/options.py

     
    1515from django.utils.text import capfirst, get_text_list
    1616from django.utils.translation import ugettext as _
    1717from django.utils.encoding import force_unicode
     18from urllib import quote
    1819import sets
    1920
    2021HORIZONTAL, VERTICAL = 1, 2
     
    394395            # Figure out where to redirect. If the user has change permission,
    395396            # redirect to the change-list page for this object. Otherwise,
    396397            # redirect to the admin index.
    397             if self.has_change_permission(request, None):
     398            if 'admin_change_return_to' in request.GET:
     399                post_url = request.GET['admin_change_return_to']
     400            elif self.has_change_permission(request, None):
    398401                post_url = '../'
    399402            else:
    400403                post_url = '../../../'
     
    459462            return HttpResponseRedirect("../add/")
    460463        else:
    461464            request.user.message_set.create(message=msg)
    462             return HttpResponseRedirect("../")
     465            if 'admin_change_return_to' in request.GET:
     466                post_url = request.GET['admin_change_return_to']
     467            else:
     468                post_url = '../'
     469            return HttpResponseRedirect(post_url)
    463470    save_change = transaction.commit_on_success(save_change)
    464471
    465472    def render_change_form(self, request, model, context, add=False, change=False, form_url='', obj=None):
     
    508515        inline_formsets = []
    509516        obj = self.model()
    510517        if request.method == 'POST':
     518            if 'admin_change_return_to' in request.GET:
     519                return_to = request.GET['admin_change_return_to']
     520            else:
     521                return_to = None
     522           
    511523            form = ModelForm(request.POST, request.FILES)
    512524            for FormSet in self.get_formsets(request):
    513525                inline_formset = FormSet(data=request.POST, files=request.FILES,
     
    520532            for FormSet in self.get_formsets(request):
    521533                inline_formset = FormSet(instance=obj)
    522534                inline_formsets.append(inline_formset)
     535            if 'HTTP_REFERER' in request.META:
     536                return_to = request.META['HTTP_REFERER']
     537            else:
     538                return_to = None
    523539
     540        if return_to:
     541            form_url = form_url + '?admin_change_return_to=%s' % quote(return_to)
     542
    524543        adminForm = AdminForm(form, list(self.get_fieldsets(request)), self.prepopulated_fields)
    525544        media = self.media + adminForm.media
    526545        for fs in inline_formsets:
     
    543562            'root_path': self.admin_site.root_path,
    544563        }
    545564        context.update(extra_context or {})
    546         return self.render_change_form(request, model, context, add=True)
     565        return self.render_change_form(request, model, context, form_url=form_url, add=True)
    547566
    548567    def change_view(self, request, object_id, extra_context=None):
    549568        "The 'change' admin view for this model."
     
    571590        ModelForm = self.get_form(request, obj)
    572591        inline_formsets = []
    573592        if request.method == 'POST':
     593            if 'admin_change_return_to' in request.GET:
     594                return_to = request.GET['admin_change_return_to']
     595            else:
     596                return_to = None
     597           
    574598            form = ModelForm(request.POST, request.FILES, instance=obj)
    575599            for FormSet in self.get_formsets(request, obj):
    576600                inline_formset = FormSet(request.POST, request.FILES, instance=obj)
     
    583607            for FormSet in self.get_formsets(request, obj):
    584608                inline_formset = FormSet(instance=obj)
    585609                inline_formsets.append(inline_formset)
     610            if 'HTTP_REFERER' in request.META:
     611                return_to = request.META['HTTP_REFERER']
     612            else:
     613                return_to = None
     614       
     615        if return_to:
     616            form_url = '?admin_change_return_to=%s' % quote(return_to)
     617        else:
     618            form_url = ''
    586619
    587620        ## Populate the FormWrapper.
    588621        #oldform = oldforms.FormWrapper(manipulator, new_data, errors)
     
    620653            'errors': AdminErrorList(form, inline_formsets),
    621654            'root_path': self.admin_site.root_path,
    622655        }
    623         context.update(extra_context or {})
    624         return self.render_change_form(request, model, context, change=True, obj=obj)
     656        context.update(extra_context or {}) 
     657        return self.render_change_form(request, model, context, change=True, form_url=form_url, obj=obj)
    625658
    626659    def changelist_view(self, request, extra_context=None):
    627660        "The 'change list' admin view for this model."
Back to Top