Login

Test if sessions are working

Author:
jpic
Posted:
March 1, 2010
Language:
Python
Version:
1.1
Tags:
sessions
Score:
-1 (after 1 ratings)

I wasn't sure if my setup supported sessions properly. This view helped me make sure.

Usage: fill the inputs with text and make sure that these values traverse a couple of requests. If it doesn't work then maybe the session backend you've set is broken?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from django import http

def test(request):
    response = 'POST:<table>'
    for k,v in request.POST.items():
        response += '<tr><td>%s</td><td>%s</td></tr>' % (k, v)
    response += '</table>'
   
    response += 'Session:<table>'
    for k,v in request.session._session.items():
        response += '<tr><td>%s</td><td>%s</td></tr>' % (k, v)
    response += '</table>'
   
    response += '''
    <form method="post" action="">
        <input type="text" name="new_key" />
        <input type="text" name="new_value" />
        <input type="submit" />
    </form>
    '''
   
    new_key = request.POST.get('new_key', False)
    new_value = request.POST.get('new_value', False)
   
    if new_key and new_value:
        request.session[new_key] = new_value
   
    return http.HttpResponse(response)

More like this

Comments

Please login first before commenting.