from django import template

register = template.Library()


@register.tag
def annotate_form_field(parser, token):
    """
    Set an attribute on a form field with the widget type

    This means templates can use the widget type to render things differently
    if they want to.  Django doesn't make this available by default.
    """
    args = token.split_contents()
    if len(args) < 2:
        raise template.TemplateSyntaxError(
            "annotate_form_field tag requires a form field to be passed")
    return FormFieldNode(args[1])


class FormFieldNode(template.Node):
    def __init__(self, field_str):
        self.field = template.Variable(field_str)

    def render(self, context):
        field = self.field.resolve(context)
        field.widget_type = field.field.widget.__class__.__name__
        return ''

from django import template

register = template.Library()


@register.tag
def annotate_form_field(parser, token):
    """
    Set an attribute on a form field with the widget type

    This means templates can use the widget type to render things differently
    if they want to.  Django doesn't make this available by default.
    """
    args = token.split_contents()
    if len(args) < 2:
        raise template.TemplateSyntaxError(
            "annotate_form_field tag requires a form field to be passed")
    return FormFieldNode(args[1])


class FormFieldNode(template.Node):
    def __init__(self, field_str):
        self.field = template.Variable(field_str)

    def render(self, context):
        field = self.field.resolve(context)
        if hasattr(field, 'field'):
            field.widget_type = field.field.widget.__class__.__name__
        return ''

Adjust form templatetag to handle missing field var
Adjust form templatetag to handle missing field var

When a non-existant field gets passed, the templatetag was raising an
unseemly AttributeError.  This change checks to see if the passed var is
actually a form field to avoid said error.

  from django import template
  
  register = template.Library()
  
  
  @register.tag
  def annotate_form_field(parser, token):
      """
      Set an attribute on a form field with the widget type
  
      This means templates can use the widget type to render things differently
      if they want to.  Django doesn't make this available by default.
      """
      args = token.split_contents()
      if len(args) < 2:
          raise template.TemplateSyntaxError(
              "annotate_form_field tag requires a form field to be passed")
      return FormFieldNode(args[1])
  
  
  class FormFieldNode(template.Node):
      def __init__(self, field_str):
          self.field = template.Variable(field_str)
  
      def render(self, context):
          field = self.field.resolve(context)
+         if hasattr(field, 'field'):
-         field.widget_type = field.field.widget.__class__.__name__
+             field.widget_type = field.field.widget.__class__.__name__
          return ''
  
Adjust form templatetag to handle missing field var
## Code Before:
from django import template

register = template.Library()


@register.tag
def annotate_form_field(parser, token):
    """
    Set an attribute on a form field with the widget type

    This means templates can use the widget type to render things differently
    if they want to.  Django doesn't make this available by default.
    """
    args = token.split_contents()
    if len(args) < 2:
        raise template.TemplateSyntaxError(
            "annotate_form_field tag requires a form field to be passed")
    return FormFieldNode(args[1])


class FormFieldNode(template.Node):
    def __init__(self, field_str):
        self.field = template.Variable(field_str)

    def render(self, context):
        field = self.field.resolve(context)
        field.widget_type = field.field.widget.__class__.__name__
        return ''

## Instruction:
Adjust form templatetag to handle missing field var
## Code After:
from django import template

register = template.Library()


@register.tag
def annotate_form_field(parser, token):
    """
    Set an attribute on a form field with the widget type

    This means templates can use the widget type to render things differently
    if they want to.  Django doesn't make this available by default.
    """
    args = token.split_contents()
    if len(args) < 2:
        raise template.TemplateSyntaxError(
            "annotate_form_field tag requires a form field to be passed")
    return FormFieldNode(args[1])


class FormFieldNode(template.Node):
    def __init__(self, field_str):
        self.field = template.Variable(field_str)

    def render(self, context):
        field = self.field.resolve(context)
        if hasattr(field, 'field'):
            field.widget_type = field.field.widget.__class__.__name__
        return ''
