Login

Admin Save and view next button

Author:
ungenio41
Posted:
May 2, 2010
Language:
Python
Version:
1.1
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. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 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.