django - 包含Django templatetag用户对象

标签 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)

当我将此代码与

{%load getusers%}
{%get_all_users as allusers%}
{%,占所有用户中的用户%}
{{用户}}
{%endfor%}

在我的模板中,渲染时出现Caught TypeError:无法哈希的类型:'list'。尤其是{%get_all_users as allusers%}导致了它。我尝试了{%for get_all_users%中的用户},它可以通过,但是什么也没打印。

追溯

追溯:
get_response中的文件“/usr/lib/python2.7/site-packages/django/core/handlers/base.py”
111. response =回调(request,* 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. return HttpResponse(loader.render_to_string(* args,** kwargs),** httpresponse_kwargs)
render_to_string中的文件“/usr/lib/python2.7/site-packages/django/template/loader.py”
188. return t.render(context_instance)
渲染中的文件“/usr/lib/python2.7/site-packages/django/template/base.py”
123. return self._render(context)
_render中的文件“/usr/lib/python2.7/site-packages/django/template/base.py”
117. return self.nodelist.render(context)
渲染中的文件“/usr/lib/python2.7/site-packages/django/template/base.py”
744. bits.append(self.render_node(node,context))
render_node中的文件“/usr/lib/python2.7/site-packages/django/template/debug.py”
73.结果= node.render(上下文)
渲染中的文件“/usr/lib/python2.7/site-packages/django/template/loader_tags.py”
127.返回created_pa​​rent._render(context)
_render中的文件“/usr/lib/python2.7/site-packages/django/template/base.py”
117. return self.nodelist.render(context)
渲染中的文件“/usr/lib/python2.7/site-packages/django/template/base.py”
744. bits.append(self.render_node(node,context))
render_node中的文件“/usr/lib/python2.7/site-packages/django/template/debug.py”
73.结果= node.render(上下文)
渲染中的文件“/usr/lib/python2.7/site-packages/django/template/loader_tags.py”
127.返回created_pa​​rent._render(context)
_render中的文件“/usr/lib/python2.7/site-packages/django/template/base.py”
117. return self.nodelist.render(context)
渲染中的文件“/usr/lib/python2.7/site-packages/django/template/base.py”
744. bits.append(self.render_node(node,context))
render_node中的文件“/usr/lib/python2.7/site-packages/django/template/debug.py”
73.结果= node.render(上下文)
渲染中的文件“/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(node,context))
render_node中的文件“/usr/lib/python2.7/site-packages/django/template/debug.py”
73.结果= node.render(上下文)
渲染中的文件“/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(node,context))
render_node中的文件“/usr/lib/python2.7/site-packages/django/template/debug.py”
73.结果= node.render(上下文)
渲染中的文件“/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] [key] =值

异常类型:/ messages / compose /中的TemplateSyntaxError
异常值:呈现时捕获TypeError:无法散列的类型:'list'

最佳答案

如果您使用的是最新的开发版本,则有一个新的标签快捷方式assignment tags,可以为您完成所有这些操作。然后,您可以这样做:

@register.assignment_tag
def get_all_users():
    return User.objects.all()

但是,代码的实际问题是要将整个参数列表传递给标记实例化:
return GetAllUsers(bits)

当您应该只传递包含变量名称的位时:
return GetAllUsers(bits[2])

最后,但是,如果这只是一个模板,那么我不明白为什么您不按照程序员手册的建议在视图中这样做。

关于django - 包含Django templatetag用户对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5880433/

相关文章:

python - 如何从代码而不是 Google App Engine 上的文件呈现 Django 模板

django - Celery+Docker+Django -- 让任务工作

python - 使用 Python 进行依赖测试

javascript - 无法接收作为 json 传输到 js 的 python 对象的属性

python - Django 模板加载器无法加载通过 pip 安装的应用程序的模板

javascript - 如何在 Django View 执行时停止它?

django - 如何使Django表单中的ChoiceField只读

html - 如何在 Django 模板中生成换行符

django - 从 django 中的外部 URL 加载模板

django - NoReverseMatch Django URL