Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Django custom template tags register.assignment_tag not working

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.

like image 381
curiousguy Avatar asked Jan 09 '14 13:01

curiousguy


People also ask

How do I register a custom template tag in Django?

Create a custom template tagUnder the application directory, create the templatetags package (it should contain the __init__.py file). For example, Django/DjangoApp/templatetags. In the templatetags package, create a . py file, for example my_custom_tags, and add some code to it to declare a custom tag.

How do I register a template tag?

Registering the tagThe tag() method takes two arguments: The name of the template tag – a string. If this is left out, the name of the compilation function will be used. The compilation function – a Python function (not the name of the function as a string).

What does {% %} mean in Django?

{% extends variable %} uses the value of variable . If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template.

Why template does not exist Django?

Django TemplateDoesNotExist error means simply that the framework can't find the template file. To use the template-loading API, you'll need to tell the framework where you store your templates. The place to do this is in your settings file ( settings.py ) by TEMPLATE_DIRS setting.


1 Answers

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.

like image 180
Flimm Avatar answered Sep 20 '22 01:09

Flimm