4π
For this you have to put the URL as a get request so that you can fetch the get values form the URL and use them in your filter to maintain your selection like:
url/?variable=value
Then in your Django view, you can access this by request.GET.get('variable')
and pass this as the context in your HTML render page then use that variable in your filter selection.
Setting variable in session:
For setting the variable in the session you can set it by:
request.session['variable'] = 'value'
and this value can be retrieve by:
if 'variable' in request.session:
variable1 = request.session['variable']
You can refer this docs.
0π
One common trick I use to do this is to use GET parameters and save directly the entire url in session (it saves time compared to saving each individual parameter individually)
class ProductList(ListView):
model = Product
paginated_by = 10
def get_queryset(self):
self.request.session['saved_product_list_url'] = self.request.get_full_path()
....
Then you can use it like this in templates :
<a href="{% if request.session.saved_product_list_url %}{{ request.session.saved_product_list_url }}{% else %}{% url 'product_list' %}{% endif %}">product list</a>
Or like this in views :
saved_product_list_url = self.request.session.get('saved_product_list_url')
if saved_product_list_url:
return redirect(saved_product_list_url)
else:
return redirect('product_list')
Also in your filter form you should add a "reset filters" like this :
<a href="{% url 'product_list' %}">reset filters</a>
- [Django]-OperationalError, Django Cannot Connect to MySQL Database
- [Django]-Changing django-storages backend from from s3 to cloudfiles and dealing with old files
- [Django]-Could not parse the remainder:
- [Django]-GetStream (Django) β Cannot Enrich Notification Feed
- [Django]-Authorization Token is not being received by Django server from ios β React native