Python Django custom template tags register.assignment_tag not working

早过忘川 提交于 2019-12-10 04:10:04

问题


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.


回答1:


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. The simple_tag() helper has gained this same ability, making the assignment_tag obsolete. Tags that use assignment_tag should be updated to use simple_tag.

Note that the behaviour of simple_tag is similar but not identical to assignment_tag in Django 1.8.




回答2:


Just ran into this during an upgrade to Django 2. The problem showed up in django-debug-toolbar but was actually corrected by updating django-webpack-loader to the latest version.



来源:https://stackoverflow.com/questions/21021690/python-django-custom-template-tags-register-assignment-tag-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!