-1

Hi on my current website (built on python+flask) homepage I have some buttons that allow the certain html elements to be changed without reloading the entire webpage, this is achieved using ajax and jquery.

However when I click onto an item and then go back it defaults me to the homepage prior to the changes, is there a way to "remember" where the user was before when going back?

So a typical user experience would look like

  • User goes to home route.

  • User clicks a button to show top selling products.

  • JQuery is listening for a click on the top selling products button and triggers an Ajax call to another update route, this update route passes back the html to display the top selling products and jquery refreshes the html shown on the front page.

  • The user then clicks on one of these products and goes to the product page, after looking at the item the user clicks back.

  • Then the user is taken to the home route again but as it is reloading home route it goes back to the "original" home route before the user clicked the top selling products button.

My question is how can I go back and have the user taken to the home page but after the jquery has updated it to show the top selling products?

repsekif
  • 51
  • 1
  • 8
  • You could use a cookie for that. – matthias_h Apr 16 '20 at 20:32
  • could you give me some idea of how that would be implemented I have never used cookies before thanks. – repsekif Apr 16 '20 at 20:44
  • I consider the answer that was given to be a better approach, but basically you could store the value of the user's choice (top products or else) in a cookie and, when the user comes back to the page, read that cookie and click the appropriate button. https://stackoverflow.com/questions/1458724/how-do-i-set-unset-a-cookie-with-jquery#answer-1458728 – matthias_h Apr 16 '20 at 21:03

1 Answers1

0

You could use flasks' sessions object. Basically, if you want to modify the sorting order on a product page, it will look something like that :

@route('/')
def index():
    render_template('index.html', sorting=session['sorting_order'])

@route('/ajax/modify_sorting_order', methods=['POST'])
def modify_sorting_order():
    session['sorting_order'] = request.form['new_sorting_order']

Of course you will also have to implement the logic with jinja html templating.

Hope it helps !

halden
  • 23
  • 4