Pergunta

I am using a form wizard and want to implement a cancel-button that brings me back to the page on which I clicked the link that brought me to the formwizard. Is there a smart way to do this? At the moment I am adding a redirect to the link that starts the form wizard like that:

<td><a href="/lecture/add/?redirect={{ request.path }}">create new lecture</a></td>

Then in get_context_data I add self.request.GET.get('redirect') to the context. In my template I then have a Cancel-Button that redirects to the link from the context. But this works only in Step 1. In all other steps the information is gone. So does anyone have an idea how to solve this? Help is much appreciated!

Foi útil?

Solução

You might want to use a hidden input field to transmit the cancel action each time the form is submitted:

<input type="hidden" name="redirect" value="{{ cancel_action }}" />

And then read it in the view:

def get_context_data(self, form, **kwargs):
    # ...
    if self.request.GET.get('redirect'):
        # ...
    elif self.request.POST.get('redirect'):
        context.update({ 'cancel_action' : self.request.POST.get('redirect') })
    # ...
    return context
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top