Ticket #6903: 6903-r9781.diff

File 6903-r9781.diff, 4.5 KB (added by Ramiro Morales, 16 years ago)

Patch updated to r9781

  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    a b  
    1717from django.utils.text import capfirst, get_text_list
    1818from django.utils.translation import ugettext as _
    1919from django.utils.encoding import force_unicode
     20from urllib import quote as urllib_quote
    2021try:
    2122    set
    2223except NameError:
     
    2627# returns the <ul> class for a given radio_admin field
    2728get_ul_class = lambda x: 'radiolist%s' % ((x == HORIZONTAL) and ' inline' or '')
    2829
     30# Query string variable where the previous URL will be passed to both change
     31# and add views so they can redirect the user back to it when they are finished
     32RETURN_URL = 'return_to'
     33
    2934class IncorrectLookupParameters(Exception):
    3035    pass
    3136
     
    469474            return HttpResponseRedirect(request.path)
    470475        else:
    471476            self.message_user(request, msg)
    472            
    473             # Figure out where to redirect. If the user has change permission,
     477
     478            # Figure out where to redirect. If we have been given an change list URL
     479            # (possibly filtered), go back to that URL. Otherwise If the user has change permission,
    474480            # redirect to the change-list page for this object. Otherwise,
    475481            # redirect to the admin index.
    476             if self.has_change_permission(request, None):
     482            if RETURN_URL in request.GET:
     483                post_url = request.GET[RETURN_URL]
     484            elif self.has_change_permission(request, None):
    477485                post_url = '../'
    478486            else:
    479487                post_url = '../../../'
     
    502510            return HttpResponseRedirect("../add/")
    503511        else:
    504512            self.message_user(request, msg)
    505             return HttpResponseRedirect("../")
    506    
     513            post_url = request.GET.get(RETURN_URL, '../')
     514            return HttpResponseRedirect(post_url)
     515
    507516    def add_view(self, request, form_url='', extra_context=None):
    508517        "The 'add' admin view for this model."
    509518        model = self.model
     
    515524        ModelForm = self.get_form(request)
    516525        formsets = []
    517526        if request.method == 'POST':
     527            return_to = request.GET.get(RETURN_URL, None)
     528
    518529            form = ModelForm(request.POST, request.FILES)
    519530            if form.is_valid():
    520531                form_validated = True
     
    550561            for FormSet in self.get_formsets(request):
    551562                formset = FormSet(instance=self.model())
    552563                formsets.append(formset)
    553        
     564
     565            return_to = request.META.get('HTTP_REFERER', None)
     566
     567        if return_to:
     568            form_url = form_url + '?%s=%s' % (RETURN_URL, urllib_quote(return_to))
     569
    554570        adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)), self.prepopulated_fields)
    555571        media = self.media + adminForm.media
    556572       
     
    573589            'app_label': opts.app_label,
    574590        }
    575591        context.update(extra_context or {})
    576         return self.render_change_form(request, context, add=True)
     592        return self.render_change_form(request, context, form_url=form_url, add=True)
    577593    add_view = transaction.commit_on_success(add_view)
    578594   
    579595    def change_view(self, request, object_id, extra_context=None):
     
    601617        ModelForm = self.get_form(request, obj)
    602618        formsets = []
    603619        if request.method == 'POST':
     620            return_to = request.GET.get(RETURN_URL, None)
     621
    604622            form = ModelForm(request.POST, request.FILES, instance=obj)
    605623            if form.is_valid():
    606624                form_validated = True
     
    628646            for FormSet in self.get_formsets(request, obj):
    629647                formset = FormSet(instance=obj)
    630648                formsets.append(formset)
    631        
     649
     650            return_to = request.META.get('HTTP_REFERER', None)
     651
     652        if return_to:
     653            form_url = '?%s=%s' % (RETURN_URL, urllib_quote(return_to))
     654        else:
     655            form_url = ''
     656
    632657        adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj), self.prepopulated_fields)
    633658        media = self.media + adminForm.media
    634659       
     
    652677            'app_label': opts.app_label,
    653678        }
    654679        context.update(extra_context or {})
    655         return self.render_change_form(request, context, change=True, obj=obj)
     680        return self.render_change_form(request, context, change=True, form_url=form_url, obj=obj)
    656681    change_view = transaction.commit_on_success(change_view)
    657682   
    658683    def changelist_view(self, request, extra_context=None):
Back to Top