【发布时间】:2011-08-18 08:03:36
【问题描述】:
嘿,我现在一直在努力解决这个问题。我正在尝试将我的用户对象传递给模板,以便我可以列出它们或列出用户名。感谢到目前为止我从这里得到的帮助。
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)
当我使用此代码时
{% 加载 getusers %} {% get_all_users 作为所有用户 %} {% for user in allusers %} {{用户}} {% endfor %}在我的模板中,我在渲染时遇到 Caught TypeError: unhashable type: 'list'。正是 {% get_all_users as allusers %} 导致了它。我尝试了 {% for user in get_all_users %},它通过但不打印任何内容。
追溯
追溯: get_response 中的文件“/usr/lib/python2.7/site-packages/django/core/handlers/base.py” 111. 响应 = 回调(请求,*callback_args,**callback_kwargs) _wrapped_view 中的文件“/usr/lib/python2.7/site-packages/django/contrib/auth/decorators.py” 23. return view_func(request, *args, **kwargs) 撰写中的文件“/home/ajunkkil/Django/basedraft/messages/views.py” 91. }, context_instance=RequestContext(request)) render_to_response 中的文件“/usr/lib/python2.7/site-packages/django/shortcuts/__init__.py” 20. 返回 HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs) render_to_string 中的文件“/usr/lib/python2.7/site-packages/django/template/loader.py” 188. 返回 t.render(context_instance) 渲染中的文件“/usr/lib/python2.7/site-packages/django/template/base.py” 123.返回self._render(上下文) _render 中的文件“/usr/lib/python2.7/site-packages/django/template/base.py” 117.返回self.nodelist.render(上下文) 渲染中的文件“/usr/lib/python2.7/site-packages/django/template/base.py” 744. bits.append(self.render_node(节点,上下文)) render_node 中的文件“/usr/lib/python2.7/site-packages/django/template/debug.py” 73. 结果 = 节点渲染(上下文) 渲染中的文件“/usr/lib/python2.7/site-packages/django/template/loader_tags.py” 127. 返回已编译的_parent._render(上下文) _render 中的文件“/usr/lib/python2.7/site-packages/django/template/base.py” 117.返回self.nodelist.render(上下文) 渲染中的文件“/usr/lib/python2.7/site-packages/django/template/base.py” 744. bits.append(self.render_node(节点,上下文)) render_node 中的文件“/usr/lib/python2.7/site-packages/django/template/debug.py” 73. 结果 = 节点渲染(上下文) 渲染中的文件“/usr/lib/python2.7/site-packages/django/template/loader_tags.py” 127. 返回已编译的_parent._render(上下文) _render 中的文件“/usr/lib/python2.7/site-packages/django/template/base.py” 117.返回self.nodelist.render(上下文) 渲染中的文件“/usr/lib/python2.7/site-packages/django/template/base.py” 744. bits.append(self.render_node(节点,上下文)) render_node 中的文件“/usr/lib/python2.7/site-packages/django/template/debug.py” 73. 结果 = 节点渲染(上下文) 渲染中的文件“/usr/lib/python2.7/site-packages/django/template/loader_tags.py” 64. 结果 = block.nodelist.render(上下文) 渲染中的文件“/usr/lib/python2.7/site-packages/django/template/base.py” 744. bits.append(self.render_node(节点,上下文)) render_node 中的文件“/usr/lib/python2.7/site-packages/django/template/debug.py” 73. 结果 = 节点渲染(上下文) 渲染中的文件“/usr/lib/python2.7/site-packages/django/template/loader_tags.py” 64. 结果 = block.nodelist.render(上下文) 渲染中的文件“/usr/lib/python2.7/site-packages/django/template/base.py” 744. bits.append(self.render_node(节点,上下文)) render_node 中的文件“/usr/lib/python2.7/site-packages/django/template/debug.py” 73. 结果 = 节点渲染(上下文) 渲染中的文件“/home/ajunkkil/Django/basedraft/messages/templatetags/getusers.py” 19. 上下文[self.varname] = User.objects.all() __setitem__ 中的文件“/usr/lib/python2.7/site-packages/django/template/context.py” 53. self.dicts[-1][key] = 值 异常类型:/messages/compose/ 处的 TemplateSyntaxError 异常值:渲染时捕获 TypeError:不可散列的类型:'list'【问题讨论】:
-
必须在所有模板中还是仅在一个模板中?
-
合二为一,它是一个用于撰写新消息的模板,我希望将用户列表放在其中。