I wrote a pretty simple template to simply dump all of my session variables out into a table:
{% if debug %}
{% block title %}Session Debug Info{% endblock %}
{% block content %}
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
{% for key, value in request.session %}
<tr>
<td>{{key}}</td>
<td>{{value}}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
{% endif %}
Unfortunately, this throws the following error:
Environment:
Request Method: GET
Request URL: http://localhost:8000/debug/session/
Django Version: 1.3
Python Version: 2.7.1
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'django.contrib.admindocs',
'django.contrib.staticfiles',
'myproject.web',
'reversion',
'south']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Template error:
In template /home/rfkrocktk/Desktop/myproject/src/myproject/web/templates/desktop/debug/session.html, error at line 13
Caught KeyError while rendering: 0
3 : {% block title %}Session Debug Info{% endblock %}
4 : {% block content %}
5 : <table class="zebra-striped">
6 : <thead>
7 : <tr>
8 : <th>Key</th>
9 : <th>Value<th>
10 : </tr>
11 : </thead>
12 : <tbody>
13 : {% for key,value in request.session %}
14 : <tr>
15 : <td>{{key}}</td>
16 : <td>{{value}}</td>
17 : </tr>
18 : {% endfor %}
19 : </tbody>
20 : </table>
21 : {% endblock %}
22 : {% endif %}
23 :
Traceback:
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/views/generic/simple.py" in direct_to_template
28. return HttpResponse(t.render(c), mimetype=mimetype)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/base.py" in render
123. return self._render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/base.py" in _render
117. return self.nodelist.render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/debug.py" in render_node
73. result = node.render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/loader_tags.py" in render
127. return compiled_parent._render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/base.py" in _render
117. return self.nodelist.render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/debug.py" in render_node
73. result = node.render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/loader_tags.py" in render
127. return compiled_parent._render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/base.py" in _render
117. return self.nodelist.render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/debug.py" in render_node
73. result = node.render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/loader_tags.py" in render
64. result = block.nodelist.render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/debug.py" in render_node
73. result = node.render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/loader_tags.py" in render
64. result = block.nodelist.render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/debug.py" in render_node
73. result = node.render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/t开发者_开发技巧emplate/defaulttags.py" in render
190. values = list(values)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/contrib/sessions/backends/base.py" in __getitem__
47. return self._session[key]
Exception Type: TemplateSyntaxError at /debug/session/
Exception Value: Caught KeyError while rendering: 0
You probably want
{% for key, value in request.session.items %}
精彩评论