Home > Software design >  Redirect the user to the previous page which uses a Primary Key (PK)
Redirect the user to the previous page which uses a Primary Key (PK)

Time:12-30

I'm currently trying to create a form that, upon success, redirects the user to the previous page (which is using a PK key).

Example of previous page : http://127.0.0.1:8000/object/update/8/

(1) What I tried:

    def get_success_url(self, **kwargs):
        return reverse_lazy('task_update:update', kwargs={'id': self.object.id})

(2) And currently I'm trying to load my form with the same pk than the precedent form (he don't need pk, example : http://127.0.0.1:8000/tag/create/8/) And then use that PK in the URL for load the precedent page like that :

    def get_success_url(self, **kwargs):
        return reverse_lazy('task_update', self.kwargs.get('pk'))

but it generates an error :

The included URLconf '8' does not appear to have any patterns in it. If you see the 'urlpatterns' variable with valid patterns in the file then the issue is probably caused by a circular import.

The goal: Globally, users start at "http://127.0.0.1:8000/object/update/8/", then they press a button which redirects them to a new page to create a tag, and then they get redirected back to the first page.

CodePudding user response:

You pass it through the named parameter kwargs=…, so:

from django.urls import reverse


def get_success_url(self, **kwargs):
    return reverse('task_update', kwargs={'pk': self.kwargs['pk']})
  • Related