django.template.TemplateSyntaxError - python examples

Here are the examples of the python api django.template.TemplateSyntaxError taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

145 Examples 7

3 View Complete Implementation : sorting_tags.py
Copyright Apache License 2.0
Author : edisonlz
    def render(self, context):
        key = self.queryset_var.var
        value = self.queryset_var.resolve(context)
        order_by = context['request'].field
        if len(order_by) > 1:
            try:
                context[key] = value.order_by(order_by)
            except template.TemplateSyntaxError:
                if INVALID_FIELD_RAISES_404:
                    raise Http404(
                        'Invalid field sorting. If DEBUG were set to False, '
                        'an HTTP 404 page would have been shown instead.')
                context[key] = value
        else:
            context[key] = value

        return ''

3 View Complete Implementation : grp_tags.py
Copyright Apache License 2.0
Author : edisonlz
def do_get_search_fields_verbose(parser, token):
    """
    Get search_fields_verbose in order to display on the Changelist.
    """
    
    try:
        tag, arg = token.contents.split(None, 1)
    except:
        raise template.TemplateSyntaxError, "%s tag requires arguments" % token.contents.split()[0]
    m = re.search(r'(.*?) as (\w+)', arg)
    if not m:
        raise template.TemplateSyntaxError, "%r tag had invalid arguments" % tag
    opts, var_name = m.groups()
    return GetSearchFields(opts, var_name)

3 View Complete Implementation : likes_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def like_widget(parser, token):
    try:
        tag_name, for_str, var_name = token.split_contents()
    except ValueError:
        tag_name = "%r" % token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a following syntax: "
            f"{{% {tag_name} for <object> %}}")
    var = template.Variable(var_name)
    return ObjectLikeWidget(var)

3 View Complete Implementation : i18n.py
Copyright Apache License 2.0
Author : edisonlz
@register.tag
def language(parser, token):
    """
    This will enable the given language just for this block.

    Usage::

        {% language "de" %}
            This is {{ bar }} and {{ boo }}.
        {% endlanguage %}

    """
    bits = token.split_contents()
    if len(bits) != 2:
        raise TemplateSyntaxError("'%s' takes one argument (language)" % bits[0])
    language = parser.compile_filter(bits[1])
    nodelist = parser.parse(('endlanguage',))
    parser.delete_first_token()
    return LanguageNode(nodelist, language)

3 View Complete Implementation : utility_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def try_to_include(parser, token):
    """
    Usage: {% try_to_include "sometemplate.html" %}

    This will fail silently if the template doesn't exist.
    If it does exist, it will be rendered with the current context.
    """
    try:
        tag_name, template_name = token.split_contents()
    except ValueError:
        tag_name = token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a single argument")
    return IncludeNode(template_name)

3 View Complete Implementation : likes_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def like_widget(parser, token):
    try:
        tag_name, for_str, var_name = token.split_contents()
    except ValueError:
        tag_name = "%r" % token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a following syntax: "
            f"{{% {tag_name} for <object> %}}")
    var = template.Variable(var_name)
    return ObjectLikeWidget(var)

3 View Complete Implementation : i18n.py
Copyright Apache License 2.0
Author : edisonlz
@register.tag("get_language_info")
def do_get_language_info(parser, token):
    """
    This will store the language information dictionary for the given language
    code in a context variable.

    Usage::

        {% get_language_info for LANGUAGE_CODE as l %}
        {{ l.code }}
        {{ l.name }}
        {{ l.name_local }}
        {{ l.bidi|yesno:"bi-directional,uni-directional" }}
    """
    args = token.split_contents()
    if len(args) != 5 or args[1] != 'for' or args[3] != 'as':
        raise TemplateSyntaxError("'%s' requires 'for string as variable' (got %r)" % (args[0], args[1:]))
    return GetLanguageInfoNode(parser.compile_filter(args[2]), args[4])

3 View Complete Implementation : utility_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def try_to_include(parser, token):
    """
    Usage: {% try_to_include "sometemplate.html" %}

    This will fail silently if the template doesn't exist.
    If it does exist, it will be rendered with the current context.
    """
    try:
        tag_name, template_name = token.split_contents()
    except ValueError:
        tag_name = token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a single argument")
    return IncludeNode(template_name)

3 View Complete Implementation : static.py
Copyright Apache License 2.0
Author : edisonlz
    @clastmethod
    def handle_token(cls, parser, token, name):
        """
        Clast method to parse prefix node and return a Node.
        """
        # token.split_contents() isn't useful here because tags using this method don't accept variable as arguments
        tokens = token.contents.split()
        if len(tokens) > 1 and tokens[1] != 'as':
            raise template.TemplateSyntaxError(
                "First argument in '%s' must be 'as'" % tokens[0])
        if len(tokens) > 1:
            varname = tokens[2]
        else:
            varname = None
        return cls(varname, name)

3 View Complete Implementation : utility_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def try_to_include(parser, token):
    """
    Usage: {% try_to_include "sometemplate.html" %}

    This will fail silently if the template doesn't exist.
    If it does exist, it will be rendered with the current context.
    """
    try:
        tag_name, template_name = token.split_contents()
    except ValueError:
        tag_name = token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a single argument")
    return IncludeNode(template_name)

3 View Complete Implementation : likes_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def like_widget(parser, token):
    try:
        tag_name, for_str, var_name = token.split_contents()
    except ValueError:
        tag_name = "%r" % token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a following syntax: "
            f"{{% {tag_name} for <object> %}}")
    var = template.Variable(var_name)
    return ObjectLikeWidget(var)

3 View Complete Implementation : likes_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def like_widget(parser, token):
    try:
        tag_name, for_str, var_name = token.split_contents()
    except ValueError:
        tag_name = "%r" % token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a following syntax: "
            f"{{% {tag_name} for <object> %}}")
    var = template.Variable(var_name)
    return ObjectLikeWidget(var)

3 View Complete Implementation : utility_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def try_to_include(parser, token):
    """
    Usage: {% try_to_include "sometemplate.html" %}

    This will fail silently if the template doesn't exist.
    If it does exist, it will be rendered with the current context.
    """
    try:
        tag_name, template_name = token.split_contents()
    except ValueError:
        tag_name = token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a single argument")
    return IncludeNode(template_name)

3 View Complete Implementation : cache.py
Copyright Apache License 2.0
Author : edisonlz
    def render(self, context):
        try:
            expire_time = self.expire_time_var.resolve(context)
        except VariableDoesNotExist:
            raise TemplateSyntaxError('"cache" tag got an unknown variable: %r' % self.expire_time_var.var)
        try:
            expire_time = int(expire_time)
        except (ValueError, TypeError):
            raise TemplateSyntaxError('"cache" tag got a non-integer timeout value: %r' % expire_time)
        vary_on = [var.resolve(context) for var in self.vary_on]
        cache_key = make_template_fragment_key(self.fragment_name, vary_on)
        value = cache.get(cache_key)
        if value is None:
            value = self.nodelist.render(context)
            cache.set(cache_key, value, expire_time)
        return value

3 View Complete Implementation : utility_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def try_to_include(parser, token):
    """
    Usage: {% try_to_include "sometemplate.html" %}

    This will fail silently if the template doesn't exist.
    If it does exist, it will be rendered with the current context.
    """
    try:
        tag_name, template_name = token.split_contents()
    except ValueError:
        tag_name = token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a single argument")
    return IncludeNode(template_name)

3 View Complete Implementation : comments.py
Copyright Apache License 2.0
Author : edisonlz
    @clastmethod
    def handle_token(cls, parser, token):
        """Clast method to parse render_comment_form and return a Node."""
        tokens = token.split_contents()
        if tokens[1] != 'for':
            raise template.TemplateSyntaxError("Second argument in %r tag must be 'for'" % tokens[0])

        # {% render_comment_form for obj %}
        if len(tokens) == 3:
            return cls(object_expr=parser.compile_filter(tokens[2]))

        # {% render_comment_form for app.models pk %}
        elif len(tokens) == 4:
            return cls(
                ctype = BaseCommentNode.lookup_content_type(tokens[2], tokens[0]),
                object_pk_expr = parser.compile_filter(tokens[3])
            )

3 View Complete Implementation : utility_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def try_to_include(parser, token):
    """
    Usage: {% try_to_include "sometemplate.html" %}

    This will fail silently if the template doesn't exist.
    If it does exist, it will be rendered with the current context.
    """
    try:
        tag_name, template_name = token.split_contents()
    except ValueError:
        tag_name = token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a single argument")
    return IncludeNode(template_name)

3 View Complete Implementation : static.py
Copyright Apache License 2.0
Author : edisonlz
    @clastmethod
    def handle_token(cls, parser, token):
        """
        Clast method to parse prefix node and return a Node.
        """
        bits = token.split_contents()

        if len(bits) < 2:
            raise template.TemplateSyntaxError(
                "'%s' takes at least one argument (path to file)" % bits[0])

        path = parser.compile_filter(bits[1])

        if len(bits) >= 2 and bits[-2] == 'as':
            varname = bits[3]
        else:
            varname = None

        return cls(varname, path)

3 View Complete Implementation : utility_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def try_to_include(parser, token):
    """
    Usage: {% try_to_include "sometemplate.html" %}

    This will fail silently if the template doesn't exist.
    If it does exist, it will be rendered with the current context.
    """
    try:
        tag_name, template_name = token.split_contents()
    except ValueError:
        tag_name = token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a single argument")
    return IncludeNode(template_name)

3 View Complete Implementation : tagging_tags.py
Copyright Apache License 2.0
Author : edisonlz
def do_tags_for_object(parser, token):
    """
    Retrieves a list of ``Tag`` objects astociated with an object and
    stores them in a context variable.

    Usage::

       {% tags_for_object [object] as [varname] %}

    Example::

        {% tags_for_object foo_object as tag_list %}
    """
    bits = token.contents.split()
    if len(bits) != 4:
        raise TemplateSyntaxError(_('%s tag requires exactly three arguments') % bits[0])
    if bits[2] != 'as':
        raise TemplateSyntaxError(_("second argument to %s tag must be 'as'") % bits[0])
    return TagsForObjectNode(bits[1], bits[3])

3 View Complete Implementation : utility_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def try_to_include(parser, token):
    """
    Usage: {% try_to_include "sometemplate.html" %}

    This will fail silently if the template doesn't exist.
    If it does exist, it will be rendered with the current context.
    """
    try:
        tag_name, template_name = token.split_contents()
    except ValueError:
        tag_name = token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a single argument")
    return IncludeNode(template_name)

3 View Complete Implementation : likes_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def like_widget(parser, token):
    try:
        tag_name, for_str, var_name = token.split_contents()
    except ValueError:
        tag_name = "%r" % token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a following syntax: "
            f"{{% {tag_name} for <object> %}}")
    var = template.Variable(var_name)
    return ObjectLikeWidget(var)

3 View Complete Implementation : utility_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def try_to_include(parser, token):
    """
    Usage: {% try_to_include "sometemplate.html" %}

    This will fail silently if the template doesn't exist.
    If it does exist, it will be rendered with the current context.
    """
    try:
        tag_name, template_name = token.split_contents()
    except ValueError:
        tag_name = token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a single argument")
    return IncludeNode(template_name)

3 View Complete Implementation : likes_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def like_widget(parser, token):
    try:
        tag_name, for_str, var_name = token.split_contents()
    except ValueError:
        tag_name = "%r" % token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a following syntax: "
            f"{{% {tag_name} for <object> %}}")
    var = template.Variable(var_name)
    return ObjectLikeWidget(var)

3 View Complete Implementation : utility_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def try_to_include(parser, token):
    """
    Usage: {% try_to_include "sometemplate.html" %}

    This will fail silently if the template doesn't exist.
    If it does exist, it will be rendered with the current context.
    """
    try:
        tag_name, template_name = token.split_contents()
    except ValueError:
        tag_name = token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a single argument")
    return IncludeNode(template_name)

3 View Complete Implementation : utility_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def try_to_include(parser, token):
    """
    Usage: {% try_to_include "sometemplate.html" %}

    This will fail silently if the template doesn't exist.
    If it does exist, it will be rendered with the current context.
    """
    try:
        tag_name, template_name = token.split_contents()
    except ValueError:
        tag_name = token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a single argument")
    return IncludeNode(template_name)

3 View Complete Implementation : likes_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def like_widget(parser, token):
    try:
        tag_name, for_str, var_name = token.split_contents()
    except ValueError:
        tag_name = "%r" % token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a following syntax: "
            f"{{% {tag_name} for <object> %}}")
    var = template.Variable(var_name)
    return ObjectLikeWidget(var)

3 View Complete Implementation : likes_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def like_widget(parser, token):
    try:
        tag_name, for_str, var_name = token.split_contents()
    except ValueError:
        tag_name = "%r" % token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a following syntax: "
            f"{{% {tag_name} for <object> %}}")
    var = template.Variable(var_name)
    return ObjectLikeWidget(var)

3 View Complete Implementation : utility_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def try_to_include(parser, token):
    """
    Usage: {% try_to_include "sometemplate.html" %}

    This will fail silently if the template doesn't exist.
    If it does exist, it will be rendered with the current context.
    """
    try:
        tag_name, template_name = token.split_contents()
    except ValueError:
        tag_name = token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a single argument")
    return IncludeNode(template_name)

3 View Complete Implementation : likes_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def like_widget(parser, token):
    try:
        tag_name, for_str, var_name = token.split_contents()
    except ValueError:
        tag_name = "%r" % token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a following syntax: "
            f"{{% {tag_name} for <object> %}}")
    var = template.Variable(var_name)
    return ObjectLikeWidget(var)

3 View Complete Implementation : comments.py
Copyright Apache License 2.0
Author : edisonlz
    @clastmethod
    def handle_token(cls, parser, token):
        """Clast method to parse render_comment_list and return a Node."""
        tokens = token.split_contents()
        if tokens[1] != 'for':
            raise template.TemplateSyntaxError("Second argument in %r tag must be 'for'" % tokens[0])

        # {% render_comment_list for obj %}
        if len(tokens) == 3:
            return cls(object_expr=parser.compile_filter(tokens[2]))

        # {% render_comment_list for app.models pk %}
        elif len(tokens) == 4:
            return cls(
                ctype = BaseCommentNode.lookup_content_type(tokens[2], tokens[0]),
                object_pk_expr = parser.compile_filter(tokens[3])
            )

3 View Complete Implementation : likes_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def like_widget(parser, token):
    try:
        tag_name, for_str, var_name = token.split_contents()
    except ValueError:
        tag_name = "%r" % token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a following syntax: "
            f"{{% {tag_name} for <object> %}}")
    var = template.Variable(var_name)
    return ObjectLikeWidget(var)

3 View Complete Implementation : i18n.py
Copyright Apache License 2.0
Author : edisonlz
@register.tag("get_current_language")
def do_get_current_language(parser, token):
    """
    This will store the current language in the context.

    Usage::

        {% get_current_language as language %}

    This will fetch the currently active language and
    put it's value into the ``language`` context
    variable.
    """
    # token.split_contents() isn't useful here because this tag doesn't accept variable as arguments
    args = token.contents.split()
    if len(args) != 3 or args[1] != 'as':
        raise TemplateSyntaxError("'get_current_language' requires 'as variable' (got %r)" % args)
    return GetCurrentLanguageNode(args[2])

3 View Complete Implementation : utility_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def try_to_include(parser, token):
    """
    Usage: {% try_to_include "sometemplate.html" %}

    This will fail silently if the template doesn't exist.
    If it does exist, it will be rendered with the current context.
    """
    try:
        tag_name, template_name = token.split_contents()
    except ValueError:
        tag_name = token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a single argument")
    return IncludeNode(template_name)

3 View Complete Implementation : tz.py
Copyright Apache License 2.0
Author : edisonlz
@register.tag("get_current_timezone")
def get_current_timezone_tag(parser, token):
    """
    Stores the name of the current time zone in the context.

    Usage::

        {% get_current_timezone as TIME_ZONE %}

    This will fetch the currently active time zone and put its name
    into the ``TIME_ZONE`` context variable.
    """
    # token.split_contents() isn't useful here because this tag doesn't accept variable as arguments
    args = token.contents.split()
    if len(args) != 3 or args[1] != 'as':
        raise TemplateSyntaxError("'get_current_timezone' requires "
                                  "'as variable' (got %r)" % args)
    return GetCurrentTimezoneNode(args[2])

3 View Complete Implementation : likes_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def like_widget(parser, token):
    try:
        tag_name, for_str, var_name = token.split_contents()
    except ValueError:
        tag_name = "%r" % token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a following syntax: "
            f"{{% {tag_name} for <object> %}}")
    var = template.Variable(var_name)
    return ObjectLikeWidget(var)

3 View Complete Implementation : i18n.py
Copyright Apache License 2.0
Author : edisonlz
@register.tag("get_current_language_bidi")
def do_get_current_language_bidi(parser, token):
    """
    This will store the current language layout in the context.

    Usage::

        {% get_current_language_bidi as bidi %}

    This will fetch the currently active language's layout and
    put it's value into the ``bidi`` context variable.
    True indicates right-to-left layout, otherwise left-to-right
    """
    # token.split_contents() isn't useful here because this tag doesn't accept variable as arguments
    args = token.contents.split()
    if len(args) != 3 or args[1] != 'as':
        raise TemplateSyntaxError("'get_current_language_bidi' requires 'as variable' (got %r)" % args)
    return GetCurrentLanguageBidiNode(args[2])

3 View Complete Implementation : likes_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def like_widget(parser, token):
    try:
        tag_name, for_str, var_name = token.split_contents()
    except ValueError:
        tag_name = "%r" % token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a following syntax: "
            f"{{% {tag_name} for <object> %}}")
    var = template.Variable(var_name)
    return ObjectLikeWidget(var)

3 View Complete Implementation : news_tags.py
Copyright Apache License 2.0
Author : edisonlz
@register.tag
def get_categories(parser,token):
	"""
		{% get_categories as <varname> %}
		{% get_categories 5 as <varname> %}
	"""
	bits = token.split_contents()
	if len(bits) == 3:
		limit = None
	elif len(bits) == 4:
		try:
			limit = abs(int(bits[1]))
		except ValueError:
			raise template.TemplateSyntaxError("If provided, second argument to `get_categories` must be a positive whole number.")
	if bits[-2].lower() != 'as':
		raise template.TemplateSyntaxError("Missing 'as' from 'get_categories' template tag.  Format is {% get_categories 5 as categories %}.")
	return CategoryNode(bits[-1], limit=limit)

3 View Complete Implementation : utility_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def try_to_include(parser, token):
    """
    Usage: {% try_to_include "sometemplate.html" %}

    This will fail silently if the template doesn't exist.
    If it does exist, it will be rendered with the current context.
    """
    try:
        tag_name, template_name = token.split_contents()
    except ValueError:
        tag_name = token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a single argument")
    return IncludeNode(template_name)

3 View Complete Implementation : news_tags.py
Copyright Apache License 2.0
Author : edisonlz
@register.tag
def months_with_news(parser, token):
	"""
		{% months_with_news 4 as months %}
	"""
	bits = token.split_contents()
	if len(bits) == 3:
		limit = None
	elif len(bits) == 4:
		try:
			limit = abs(int(bits[1]))
		except ValueError:
			raise template.TemplateSyntaxError("If provided, second argument to `months_with_news` must be a positive whole number.")
	if bits[-2].lower() != 'as':
		raise template.TemplateSyntaxError("Missing 'as' from 'months_with_news' template tag.	Format is {% months_with_news 5 as months %}.")
	return MonthNode(bits[-1], limit=limit)

3 View Complete Implementation : utility_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def try_to_include(parser, token):
    """
    Usage: {% try_to_include "sometemplate.html" %}

    This will fail silently if the template doesn't exist.
    If it does exist, it will be rendered with the current context.
    """
    try:
        tag_name, template_name = token.split_contents()
    except ValueError:
        tag_name = token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a single argument")
    return IncludeNode(template_name)

3 View Complete Implementation : news_tags.py
Copyright Apache License 2.0
Author : edisonlz
@register.tag
def get_news(parser, token):
	"""
		{% get_news 5 as news_items %}
	"""
	bits = token.split_contents()
	if len(bits) == 3:
		limit = None
	elif len(bits) == 4:
		try:
			limit = abs(int(bits[1]))
		except ValueError:
			raise template.TemplateSyntaxError("If provided, second argument to `get_news` must be a positive whole number.")
	if bits[-2].lower() != 'as':
		raise template.TemplateSyntaxError("Missing 'as' from 'get_news' template tag.	Format is {% get_news 5 as news_items %}.")
	return NewsItemNode(bits[-1], limit)

3 View Complete Implementation : likes_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def like_widget(parser, token):
    try:
        tag_name, for_str, var_name = token.split_contents()
    except ValueError:
        tag_name = "%r" % token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a following syntax: "
            f"{{% {tag_name} for <object> %}}")
    var = template.Variable(var_name)
    return ObjectLikeWidget(var)

3 View Complete Implementation : likes_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def like_widget(parser, token):
    try:
        tag_name, for_str, var_name = token.split_contents()
    except ValueError:
        tag_name = "%r" % token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a following syntax: "
            f"{{% {tag_name} for <object> %}}")
    var = template.Variable(var_name)
    return ObjectLikeWidget(var)

3 View Complete Implementation : likes_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def like_widget(parser, token):
    try:
        tag_name, for_str, var_name = token.split_contents()
    except ValueError:
        tag_name = "%r" % token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a following syntax: "
            f"{{% {tag_name} for <object> %}}")
    var = template.Variable(var_name)
    return ObjectLikeWidget(var)

3 View Complete Implementation : likes_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def like_widget(parser, token):
    try:
        tag_name, for_str, var_name = token.split_contents()
    except ValueError:
        tag_name = "%r" % token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a following syntax: "
            f"{{% {tag_name} for <object> %}}")
    var = template.Variable(var_name)
    return ObjectLikeWidget(var)

3 View Complete Implementation : likes_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def like_widget(parser, token):
    try:
        tag_name, for_str, var_name = token.split_contents()
    except ValueError:
        tag_name = "%r" % token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a following syntax: "
            f"{{% {tag_name} for <object> %}}")
    var = template.Variable(var_name)
    return ObjectLikeWidget(var)

3 View Complete Implementation : likes_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def like_widget(parser, token):
    try:
        tag_name, for_str, var_name = token.split_contents()
    except ValueError:
        tag_name = "%r" % token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a following syntax: "
            f"{{% {tag_name} for <object> %}}")
    var = template.Variable(var_name)
    return ObjectLikeWidget(var)

3 View Complete Implementation : utility_tags.py
Copyright MIT License
Author : PacktPublishing
@register.tag
def try_to_include(parser, token):
    """
    Usage: {% try_to_include "sometemplate.html" %}

    This will fail silently if the template doesn't exist.
    If it does exist, it will be rendered with the current context.
    """
    try:
        tag_name, template_name = token.split_contents()
    except ValueError:
        tag_name = token.contents.split()[0]
        raise template.TemplateSyntaxError(
            f"{tag_name} tag requires a single argument")
    return IncludeNode(template_name)