Can I use a middleware to preserve several user selected choices in between page requests?
I have several values, namely vehicle year, make, model, series, style, color, and transmission. I want to have the user being able to select a choice while keeping the previously selected choice active. I do not want to use sessions for this as I want the URL to be bookmarkable.
I was thinking of something like:
def get_choices(self):
try:
return self.REQUEST["year"]
except (KeyError, ValueError, TypeError):
return 1
class ChoicesMiddleware(object):
def process_request(self, request):
.....
I'm also not sure how to return all the choices under get_choices().
EDIT 1
def get_choices(self):
user_choices = {}
for key in ["year", "model", "make", "series", "style"]:
user_choices[key] = self.REQUEST.get(key)
return user_choices
class ChoicesMiddleware(object):
def process_request(self, request):
return get_choices(self)
EDIT 2 My URLConf is as below:
(r'^inventory/(?P<year>\d{4})(?P<make>[-\w\s]+)
(?P<model>[-\w\s]+)(?P<series>[-\w\s]+)(?P<body>[-\w\s]+)
(?P<exterior>[-\w\s]+)(?P<interior>[-\w\s]+)
(?P<transmission>[-\w\s]+)$', 'inventory'),
Then the view is as below:
def inventory(request, page_by=None, year=None, make=None,
model=None, series=None, body=None, interior=None, exterior=None,
transmission=None):
#Initialize empty variable list.
kwargs = {}
if "year" in request.GET:
year = request.REQUEST["year"]
kwargs['common_vehicle__year__year__exact'] = year
....The rest of the vars are populated in the same way.