I hate to request more changes to magic-removal... but that's where
this should happen. Maybe it should wait until after it's been merged
though.

Currently the redirects after add_stage and change_stage in the admin
system are hardcoded. One of the most frequent requests I get in my
projects is to allow people to go to some other related object after
they save, not back to the list of objects. The redirect should be
customizable. Here's how I propose it should be done.

Add a template block {% block submitrow %} around the submit_row in
the admin/change_form.html. This would allow people to override the
save buttons. It would be cool to do this with a combination of
template tags, and attributes in the inner Admin class, but a block
would still be useful to have. If anyone has suggestions on how
attributes+template tags might work, I'd like to hear them. I haven't
thought about it too much yet.

By default, the inner Admin class should have a couple of new methods
(or attributes that are assigned to a callable) One callable returns
an HttpResponseRedirect for adding, and the other, after changing.
These callables would take the request and the new (or updated) object
as arguments. So the default for change_stage would look like:

def after_change_action(request, new_object):
    pk_value = getattr(new_object, opts.pk.attname)
    if request.POST.has_key("_continue"):
        request.user.add_message(msg + ' ' + _("You may edit it again below."))
        if request.REQUEST.has_key('_popup'):
            return HttpResponseRedirect(request.path + "?_popup=1")
        else:
            return HttpResponseRedirect(request.path)
    elif request.POST.has_key("_saveasnew"):
        request.user.add_message(_('The %(name)s "%(obj)s" was added
successfully. You may edit it again below.') % {'name':
opts.verbose_name, 'obj': new_object})
        return HttpResponseRedirect("../../%s/" % pk_value)
    elif request.POST.has_key("_addanother"):
        request.user.add_message(msg + ' ' + (_("You may add another
%s below.") % opts.verbose_name))
        return HttpResponseRedirect("../../add/")
    else:
        request.user.add_message(msg)
        return HttpResponseRedirect("../../")

Another option would be to have the callable return a (url, message)
tuple, and let the view handle HttpResponseRedirect and
request.user.add_message. I think it depends on whether people think
it would be useful to do anything but a redirect on successful
adding/editing of an object using the admin system.

It would be nice to be able to override log_change_message in a
similar way as well, but I think rjwittams has something event based
in mind for that. I could be wrong.

At any rate, I think the new inner Admin class is a clean place to put
this kind of stuff.

Joseph

Reply via email to