Return to the page I has before

i have a table with a list of products and a button that redirectes me to other page to edit that product like product/edit/. When i finished edit the product i want to cameback to the page i was in before i went to the edit page. I cant use redirect and the url code because i am using django-filters and i can acess the edit page from other pages too.

So i have 2 possible starts:
/product?active=yes
/product/detail/
there i can acess the edit page
/product/edit/
and when i save the product instance i want to came back to or product list or the detail based on i has before?

It is hard to do so ? I need to do it in some pages :frowning:

Thanks for anyhelp!

1 Like

You have a couple of different options.

You could create a hidden field in the edit form to contain the url to return to - and when the form is submitted, use it as the target for the redirect.

Or, you could create a session variable that gets populated when you go to the edit page and get that variable from the session when the edit page is submitted.

Neither is a “perfect” solution, but either one should be adequate in the general case.

1 Like

I was able to do the first option, but i am not entirely happy with that solution.
How session variable work ? Where do i define them ?

See the docs at How to use sessions | Django documentation | Django

There is another alternative to the 2 mentioned already and that is to pass the back url on as a variable in the URL which you can then set as the success url in the edit view.

Thanks you both for your help!

Hey mates,

I was wordering if it was possible to use global variables, like, create a variable in the views.py file and update that variable inside one of the views ?

No.

Global variables is an absolute No-No.

See I'm new to WebDev, explain to me why or why it wouldn't be OK for all users to access a global Lark() parser object? for more on why this is fundamentally wrong.

2 Likes

Maybe could invent a library that would allow one to code Python as usual, then some intermediate service converts it to proper Django without globals. Would require heavy grammar/ formal language skills or deep knowledge of Python AST.