lef*_*ffe 5 django django-templates
嘿,我现在一直在为这个问题苦苦挣扎。我试图将我的用户对象传递给模板,以便我可以列出它们或列出用户名。感谢到目前为止我从这里得到的帮助,我得到了这个。
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)
Run Code Online (Sandbox Code Playgroud)
当我使用这段代码时
{% 加载 getuser %}
{% get_all_users 作为所有用户 %}
{% for allusers %}
{{ 用户 }}
{% 结束 %}
在我的模板中,我在渲染时遇到了 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(请求, *args, **kwargs) 在 compose 中文件“/home/ajunkkil/Django/basedraft/messages/views.py” 91. }, context_instance=RequestContext(请求)) 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(context) 渲染中的文件“/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. 结果=node.render(context) 渲染中的文件“/usr/lib/python2.7/site-packages/django/template/loader_tags.py” 127.返回compile_parent._render(上下文) _render 中的文件“/usr/lib/python2.7/site-packages/django/template/base.py” 117. 返回 self.nodelist.render(context) 渲染中的文件“/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. 结果=node.render(context) 渲染中的文件“/usr/lib/python2.7/site-packages/django/template/loader_tags.py” 127.返回compile_parent._render(上下文) _render 中的文件“/usr/lib/python2.7/site-packages/django/template/base.py” 117. 返回 self.nodelist.render(context) 渲染中的文件“/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. 结果=node.render(context) 渲染中的文件“/usr/lib/python2.7/site-packages/django/template/loader_tags.py” 64. 结果 = block.nodelist.render(context) 渲染中的文件“/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. 结果=node.render(context) 渲染中的文件“/usr/lib/python2.7/site-packages/django/template/loader_tags.py” 64. 结果 = block.nodelist.render(context) 渲染中的文件“/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. 结果=node.render(context) 渲染中的文件“/home/ajunkkil/Django/basedraft/messages/templatetags/getusers.py” 19. context[self.varname] = User.objects.all() __setitem__ 中的文件“/usr/lib/python2.7/site-packages/django/template/context.py” 53. self.dicts[-1][键] = 值 异常类型:/messages/compose/ 处的 TemplateSyntaxError 异常值:渲染时捕获类型错误:不可散列类型:'list'
如果您使用的是最新的开发版本,则有一个新的标签快捷方式,分配标签,它可以为您完成所有这一切。然后你可以这样做:
@register.assignment_tag
def get_all_users():
return User.objects.all()
Run Code Online (Sandbox Code Playgroud)
不过,代码的实际问题是您将整个参数列表传递给标签实例化:
return GetAllUsers(bits)
Run Code Online (Sandbox Code Playgroud)
当您应该只传递包含变量名称的位时:
return GetAllUsers(bits[2])
Run Code Online (Sandbox Code Playgroud)
然而,最后,如果这只是针对一个模板,我不明白为什么你不按照程序员手册的建议在视图中执行此操作。
| 归档时间: |
|
| 查看次数: |
2094 次 |
| 最近记录: |