save to session django code example
Example 1: django sessions for beginners
def index(request):
...
num_authors = Author.objects.count()
num_visits = request.session.get('num_visits', 0)
request.session['num_visits'] = num_visits + 1
context = {
'num_books': num_books,
'num_instances': num_instances,
'num_instances_available': num_instances_available,
'num_authors': num_authors,
'num_visits': num_visits,
}
return render(request, 'index.html', context=context)
Example 2: django flush sessions on server startup
#!/bin/sh
DJANGO_SETTINGS_MODULE="myproj.settings" \
python -c 'from django.contrib.sessions.models import Session; \
Session.objects.all().delete()'
python manage.py runserver