Login

Admin Save and view next button

Author:
ungenio41
Posted:
May 2, 2010
Language:
Python
Version:
1.1
Tags:
admin view save button next 1.2
Score:
2 (after 2 ratings)

Add a Save and view next button to your admin change form.

Put the code at link in a file called myapp/templates/admin/myapp/change_form.html

Note: Requires Django 1.2.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
"""admin.py"""
from django.contrib import admin
from django.http import HttpResponseRedirect
from django.utils.encoding import force_unicode
from django.utils.translation import ugettext_lazy as _
from myapp.models import MyModel


class MyAdmin(admin.ModelAdmin):
    def response_change(self, request, obj):
        """
        Determines the HttpResponse for the change_view stage.
        """
        if request.POST.has_key("_viewnext"):
            msg = (_('The %(name)s "%(obj)s" was changed successfully.') %
                   {'name': force_unicode(obj._meta.verbose_name),
                    'obj': force_unicode(obj)})
            next = obj.__class__.objects.filter(id__gt=obj.id).order_by('id')[:1]
            if next:
                self.message_user(request, msg)
                return HttpResponseRedirect("../%s/" % next[0].pk)
        return super(MyAdmin, self).response_change(request, obj)


admin.site.register(MyModel, MyAdmin)

More like this

  1. Admin Save and view next button html by ungenio41 5 years ago
  2. URLField admin widget with View Link button by ungenio41 5 years ago
  3. Add buttons in admin forms by mamat 6 years, 8 months ago
  4. Button Admin by delfick 5 years, 4 months ago
  5. Admin page without inlines dinamically by davmuz 2 years, 11 months ago

Comments

gamesbook (on December 11, 2012):

The problem is that this does not test where the "next" item is in terms of the existing ordering.

#

Please login first before commenting.