shiva
shiva

Reputation: 2770

custom template tag function not calling

Template code:

{% extends 'some.html' %}
{% load tag %}
{% get_rate land contDetails.postcode contDetails.county title uid LsAff.aff_id LsAff.group_id %}

custom template tag:

 from django import template
 from process.utils.error_handler import  debug_logger
 from django.template import Library, Node, TemplateSyntaxError
 class land(template.Node):
   def __init__(self, var):
      self.varname = template.Variable(var)
      debug_logger().info(self.varname)

   def render(self, context):
     debug_logger().info("hello")
     user = self.varname.resolve(context)
     debug_logger().info("hello")
     debug_logger().info(user)
     return "somestring"

def get_rate(parser, token):
   debug_logger().info("hell")
   bits=token.split_contents()
   var=bits[2]
   debug_logger().info(var)
   return land(var)
register = template.Library()
register.tag('get_rate', get_rate)

In the above code def render(self, context): function is not calling. Till debug_logger().info(self.varname) statement the code works properly.

Am i missing anything? please help to find the solution to call render(self, context)

Upvotes: 0

Views: 1520

Answers (1)

Brandon Taylor
Brandon Taylor

Reputation: 34553

I set up a quick view function and replicated your tag. I reduced the number of arguments to two for brevity. Everything is working ok for me.

#views.py
from django.shortcuts import render

def test_view(request):
    return render(request, 'test.html', {'var' : True})

#tag.py
from django import template

register = template.Library()

@register.tag('get_rate')
def get_rate(parser, token):
    bits = token.contents.split()
    var = bits[2]
    return LandNode(var)

class LandNode(template.Node):
    def __init__(self, var):
        self.varname = template.Variable(var)

    def render(self, context):
        varname = self.varname.resolve(context)
        return 'some string'

#test.html
{% load tag %}
{% get_rate 'foo' var %}

Renders:

some string

I would recommend reducing the method signature on that template tag and just pass in objects where you're passing in multiple object properties.

Upvotes: 1

Related Questions