def set_units(request): if request.method == 'POST': form = UnitChoiceForm(data=request.POST) if form.is_valid(): unit_choice = form.cleaned_data['unit_choice'] request.session['user_units'] = unit_choice return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
def get_user_units(request): user_units = request.session.get('user_units') if not user_units: user_units = DEFAULT_USER_UNIT request.session['user_units'] = DEFAULT_USER_UNIT if user_units not in UNIT_CHOICES_SET: # shouldn't be possible, defensive check to fail gracefully user_units = DEFAULT_USER_UNIT # tcp = template context processor, added to prevent namespace collisions return { 'tcp__user_units': user_units, 'tcp__unit_choice_form': UnitChoiceForm(initial={'unit_choice': user_units}), }