Python Django自定义模板标签register.assignment_tag不起作用 [英] Python Django custom template tags register.assignment_tag not working
问题描述
这是我的Python Django自定义模板代码代码
来自django导入模板
,来自ipc.declarations。模型从django导入MainDeclaration
.shortcuts导入get_object_or_404
寄存器= template.Library()
def section_settings(declarationId,user):
声明= get_object_or_404(MainDeclaration,pk = declarationId,user = user)
businessInfo =声明.GetOrCreateBusinessInfo()
section =声明.GetSections()
返回节
register.assignment_tag(section_settings)
现在我遇到了错误
register.assignment_tag(section_settings)
[Thu Jan 09 06:50:44 2014] [错误] [客户端127.0.0.1] AttributeError: '库'对象没有属性'assignment_tag'
这与我的开发服务器应用程序正常工作,但在上载相同代码时不起作用生产服务r。
请指导我。
即使这个问题已有几岁了,最近人们可能再次看到此错误,但原因有所不同。再次出现错误:
AttributeError:库对象没有属性 assignment_tag
升级到Django 2.0后,您可能会看到此错误。这是因为 assignment_tag
在Django 1.9中已弃用,在Django 2.0中已删除:
Django 1.4添加了
assignment_tag
帮助程序,以简化模板结果的创建,该结果将结果存储在模板变量中。simple_tag()
助手具有相同的功能,从而使assignment_tag
过时。使用assignment_tag
的标签应更新为使用simple_tag
。
请注意, simple_tag
的行为类似于但不等同于 assignment_tag
Django 1.8。
This is my Code for Python Django custom template tags
from django import template
from ipc.declarations.models import MainDeclaration
from django.shortcuts import get_object_or_404
register = template.Library()
def section_settings(declarationId,user):
declaration = get_object_or_404(MainDeclaration, pk=declarationId, user=user)
businessInfo = declaration.GetOrCreateBusinessInfo()
sections = declaration.GetSections()
return sections
register.assignment_tag(section_settings)
Now i am getting a error
register.assignment_tag(section_settings)
[Thu Jan 09 06:50:44 2014] [error] [client 127.0.0.1] AttributeError: 'Library' object has no attribute 'assignment_tag'
This works fine with my development server application , but not working while uploading the same code in Production server.
Please guide me.
Even though this question is a few years old, people recently may be seeing this error again, but for a different reason. Here's the error again:
AttributeError: 'Library' object has no attribute 'assignment_tag'
You may be seeing this error after upgrading to Django 2.0. This is because assignment_tag
was deprecated in Django 1.9, and removed in Django 2.0:
Django 1.4 added the
assignment_tag
helper to ease the creation of template tags that store results in a template variable. Thesimple_tag()
helper has gained this same ability, making theassignment_tag
obsolete. Tags that useassignment_tag
should be updated to usesimple_tag
.
Note that the behaviour of simple_tag
is similar but not identical to assignment_tag
in Django 1.8.
这篇关于Python Django自定义模板标签register.assignment_tag不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!