Current redirection URL after saving a DX edit/add form?

We have a browser view with a a list of links to other items (their @@edit view in particular. After clicking on the edit link and after saving/cancelling the edit form the browser should redirect to the original view (or a different URL specified somehow) instead of the @@view of the related context object...any idea how to solve this?


I got inspiration from this:

You have to add a custom add form.

First you add a named adapter for the content type in my case that is cru with zcml:

    <class class=".add_cru.CruAddView">

pointing to the class:

from Acquisition import aq_inner
from plone.dexterity.browser import add
from plone.dexterity.utils import addContentToContainer

class CruAddForm(add.DefaultAddForm):
    portal_type = 'cru'

    def add(self, object):
        container = aq_inner(self.context)
        addContentToContainer(container, object, checkConstraints=False) #you only need checkConstraints=False for anonymous add
        self.immediate_view = container.absolute_url() #redirect here

class CruAddView(add.DefaultAddView):
    form = CruAddForm

For the edit form you probably have to look at, the zcml would probably be different in that case.

This looks so scary but I will give it a try :smile:

In case like this I override the nextURL method in the add and edit view:

Follows an example taken from a wizard edit form:

seems about right, but you also need to override the add method of add if you want it to redirect to a different view on add as well as it doesn't seem to use nextURL to handle redirect.

Not as scary and not as tricky as I thought.

The trick was to add a return_url=.... parameter to the related edit links and pick up the return_url parameter inside the nextURL() method from request/HTTP_REFERER.


1 Like

But the return URL will be lost if the form is submitted with validation errors, unless you hang on to it in a hidden input.

David Glick