diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
a
|
b
|
|
17 | 17 | from django.utils.text import capfirst, get_text_list |
18 | 18 | from django.utils.translation import ugettext as _ |
19 | 19 | from django.utils.encoding import force_unicode |
| 20 | from urllib import quote as urllib_quote |
20 | 21 | try: |
21 | 22 | set |
22 | 23 | except NameError: |
… |
… |
|
26 | 27 | # returns the <ul> class for a given radio_admin field |
27 | 28 | get_ul_class = lambda x: 'radiolist%s' % ((x == HORIZONTAL) and ' inline' or '') |
28 | 29 | |
| 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 |
| 32 | RETURN_URL = 'return_to' |
| 33 | |
29 | 34 | class IncorrectLookupParameters(Exception): |
30 | 35 | pass |
31 | 36 | |
… |
… |
|
469 | 474 | return HttpResponseRedirect(request.path) |
470 | 475 | else: |
471 | 476 | 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, |
474 | 480 | # redirect to the change-list page for this object. Otherwise, |
475 | 481 | # 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): |
477 | 485 | post_url = '../' |
478 | 486 | else: |
479 | 487 | post_url = '../../../' |
… |
… |
|
502 | 510 | return HttpResponseRedirect("../add/") |
503 | 511 | else: |
504 | 512 | self.message_user(request, msg) |
505 | | return HttpResponseRedirect("../") |
506 | | |
| 513 | post_url = request.GET.get(RETURN_URL, '../') |
| 514 | return HttpResponseRedirect(post_url) |
| 515 | |
507 | 516 | def add_view(self, request, form_url='', extra_context=None): |
508 | 517 | "The 'add' admin view for this model." |
509 | 518 | model = self.model |
… |
… |
|
515 | 524 | ModelForm = self.get_form(request) |
516 | 525 | formsets = [] |
517 | 526 | if request.method == 'POST': |
| 527 | return_to = request.GET.get(RETURN_URL, None) |
| 528 | |
518 | 529 | form = ModelForm(request.POST, request.FILES) |
519 | 530 | if form.is_valid(): |
520 | 531 | form_validated = True |
… |
… |
|
550 | 561 | for FormSet in self.get_formsets(request): |
551 | 562 | formset = FormSet(instance=self.model()) |
552 | 563 | 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 | |
554 | 570 | adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)), self.prepopulated_fields) |
555 | 571 | media = self.media + adminForm.media |
556 | 572 | |
… |
… |
|
573 | 589 | 'app_label': opts.app_label, |
574 | 590 | } |
575 | 591 | 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) |
577 | 593 | add_view = transaction.commit_on_success(add_view) |
578 | 594 | |
579 | 595 | def change_view(self, request, object_id, extra_context=None): |
… |
… |
|
601 | 617 | ModelForm = self.get_form(request, obj) |
602 | 618 | formsets = [] |
603 | 619 | if request.method == 'POST': |
| 620 | return_to = request.GET.get(RETURN_URL, None) |
| 621 | |
604 | 622 | form = ModelForm(request.POST, request.FILES, instance=obj) |
605 | 623 | if form.is_valid(): |
606 | 624 | form_validated = True |
… |
… |
|
628 | 646 | for FormSet in self.get_formsets(request, obj): |
629 | 647 | formset = FormSet(instance=obj) |
630 | 648 | 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 | |
632 | 657 | adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj), self.prepopulated_fields) |
633 | 658 | media = self.media + adminForm.media |
634 | 659 | |
… |
… |
|
652 | 677 | 'app_label': opts.app_label, |
653 | 678 | } |
654 | 679 | 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) |
656 | 681 | change_view = transaction.commit_on_success(change_view) |
657 | 682 | |
658 | 683 | def changelist_view(self, request, extra_context=None): |