Login

Test if sessions are working

Author:
jpic
Posted:
March 1, 2010
Language:
Python
Version:
1.1
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

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 8 months ago

Comments

Please login first before commenting.