Hey I have been struggling with this one alot now. I am trying to pass my user objects to a template so that I can list them, or list usernames. Thanks to the help I've got from here so far I have this.
from django.template import Library, Node, Template, VariableDoesNotExist, TemplateSyntaxError, \
Variable
from django.utils.translation import ugettext as _
from django.contrib.auth.models import User
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db import models
register = Library()
class GetAllUsers(Node):
def __init__(self, varname):
# Save the variable that we will assigning the users to
self.varname = varname
def render(self, context):
# Save all the user objects to the variable and return the context to the template
context[self.varname] = User.objects.all()
return ''
@register.tag(name="get_all_users")
def get_all_users(parser, token):
# First break up the arguments that have been passed to the template tag
bits = token.contents.split()
if len(bits) != 3:
raise TemplateSyntaxError, "get_all_users tag takes exactly 2 arguments"
if bits[1] != 'as':
raise TemplateSyntaxError, "1st argument to get_all_users tag must be 'as'"
return GetAllUsers(bits)
#register.tag('get_all_users', get_all_users)
When I use this code with
{% load getusers %} {% get_all_users as allusers %} {% for user in allusers %} {{ user }} {% endfor %}
In my template I get Caught TypeError while rendering: unhashable type: 'list'. And it is specifically {% get_all_users as allusers %} thats causing it. I tried {% for user in get_all_users %}, it goes through but doesn't print anything.
Traceback
Traceback: File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs) File "/usr/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view 23. return view_func(request, *args, **kwargs) File "/home/ajunkkil/Django/basedraft/messages/views.py" in compose 91. }, context_instance=RequestContext(request)) File "/usr/lib/python2.7/site-packages/django/shortcuts/__init__.py" in render_to_response 20. return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs) File "/usr/lib/python2.7/site-packages/django/template/loader.py" in render_to_string 188. return t.render(context_instance) File "/usr/lib/python2.7/site-packages/django/template/base.py" in render 123. return self._render(context) File "/usr/lib/python2.7/site-packages/django/template/base.py" in _render 117. return self.nodelist.render(context) File "/usr/lib/python2.7/site-packages/django/template/base.py" in render 744. bits.append(self.render_node(node, context)) File "/usr/lib/python2.7/site-packages/django/template/debug.py" in render_node 73. result = node.render(context) File "/usr/lib/python2.7/site-packages/django/template/loader_tags.py" in render 127. return compiled_parent._render(context) File "/usr/lib/python2.7/site-packages/django/template/base.py" in _render 117. return self.nodelist.render(context) File "/usr/lib/python2.7/site-packages/django/template/base.py" in render 744. bits.append(self.render_node(node, context)) File "/usr/lib/python2.7/site-packages/django/template/debug.py" in render_node 73. result = node.render(context) File "/usr/lib/python2.7/site-packages/django/template/loader_tags.py" in render 127. return compiled_parent._render(context) File "/usr/lib/python2.7/site-packages/django/template/base.py" in _render 117. return self.nodelist.render(context) File "/usr/lib/python2.7/site-packages/django/template/base.py" in render 744. bits.append(self.render_node(node, context)) File "/usr/lib/python2.7/site-packages/django/template/debug.py" in render_node 73. result = node.render(context) File "/usr/lib/python2.7/site-packages/django/template/loader_tags.py" in render 64. result = block.nodelist.render(context) File "/usr/lib/python2.7/site-packages/django/template/base.py" in render 744. bits.append(self.render_node(node, context)) File "/usr/lib/python2.7/site-packages/django/template/debug.py" in render_node 73. result = node.render(context) File "/usr/lib/python2.7/site-packages/django/template/loader_tags.py" in render 64. result = block.nodelist.render(context) File "/usr/lib/python2.7/site-packages/django/template/base.py" in render 744. bits.append(self.render_node(node, context)) File "/usr/lib/python2.7/site-packages/django/template/debug.py" in render_node 73. result = node.render(context) File "/home/ajunkkil/Django/basedraft/messages/templatetags/getusers.py" in render 19. context[self.varname] = User.objects.all() File "/usr/lib/python2.7/site-packages/django/template/context.py" in __setitem__ 53. self.dicts[-1][key] = value Exception Type: TemplateSyntaxError at /messages/compose/ Exception Value: Caught TypeError while rendering: unhashable type: 'list'