[Answer]-Django how to keep GET data?

1👍

I see two options.

1: Use Django’s session framework.

When you receive a request for filtering, store the filter options in the session: request.session['filter_options'] = filter_option_dict

Then, when you load the page and don’t see any filtering options in request.GET, check if there are any options in the session:

if(len(request.GET) > 0):
    #get filter options from request.GET
    #save filter options to session
elif('filter_options' in request.session):
    #get filter options from session
else:
    #no filter options, display without filtering

Note that the session is designed for temporary data, and is specific to one browser on one computer. That’s not a problem for this use case, but it’s something to remember.

2: Use ajax to load the message without changing the rest of the page. On the server, you create a view that returns a message without the page header or any other formatting, and javascript on the front-end to send requests and insert the retrieved messages into the page. This is very easy with JQuery, although it can be done without.

This would be my preferred solution, as websites that update themselves interactively feel much nicer from a user’s perspective than one that has to refresh every time they click a link.

Leave a comment