Python django.template.base.TemplateSyntaxError() Examples

The following are 21 code examples of django.template.base.TemplateSyntaxError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.template.base , or try the search function .
Example #1
Source File: thumbnails.py    From python-thumbnails with MIT License 6 votes vote down vote up
def __init__(self, parser, token):
        tokens = token.split_contents()
        self.original = parser.compile_filter(tokens[1])
        self.size = parser.compile_filter(tokens[2])
        self.options = {}

        if tokens[-2] == 'as':
            self.variable_name = tokens[-1]
        else:
            raise TemplateSyntaxError('get_thumbnail tag needs an variable assignment with "as"')

        for option in tokens[3:-2]:
            parsed_option = re.match(r'^(?P<key>[\w]+)=(?P<value>.+)$', option.strip())
            if parsed_option:
                key = parsed_option.group('key')
                value = parser.compile_filter(parsed_option.group('value'))
                self.options[key] = value
            else:
                raise TemplateSyntaxError('{} is invalid option syntax'.format(option)) 
Example #2
Source File: permissions.py    From django-is-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def has_permission(parser, token):
    bits = list(token.split_contents())
    if len(bits) < 2:
        raise TemplateSyntaxError("%r takes minimal one argument" % bits[0])
    end_tag = 'end' + bits[0]

    vals = []
    for bit in bits[2:]:
        vals.append(parser.compile_filter(bit))

    nodelist_true = parser.parse(('else', end_tag))
    token = parser.next_token()
    if token.contents == 'else':
        nodelist_false = parser.parse((end_tag,))
        parser.delete_first_token()
    else:
        nodelist_false = NodeList()
    return PermissionNode(parser.compile_filter(bits[1]), vals, nodelist_true, nodelist_false) 
Example #3
Source File: test_basic.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_basic_syntax08(self):
        """
        Raise TemplateSyntaxError for empty variable tags.
        """
        with self.assertRaisesMessage(TemplateSyntaxError, 'Empty variable tag on line 1'):
            self.engine.get_template('basic-syntax08') 
Example #4
Source File: forms.py    From django-is-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def do_form_renderer(parser, token):
    bits = token.split_contents()
    if len(bits) < 2:
        raise TemplateSyntaxError("%r tag takes at least one argument: the form that will be rendered" % bits[0])
    remaining_bits = bits[2:]
    options = token_kwargs(remaining_bits, parser, support_legacy=False)
    template_name = options.get('template', parser.compile_filter("'is_core/forms/default_form.html'"))
    options['use_csrf'] = options.get('use_csrf', parser.compile_filter('True'))
    options['form'] = parser.compile_filter(bits[1])
    options['method'] = options.get('method', parser.compile_filter("'POST'"))
    return FormNode(template_name, extra_context=options) 
Example #5
Source File: permissions.py    From django-is-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def has_permission_to_url(parser, token):
    bits = list(token.split_contents())
    if len(bits) < 2:
        raise TemplateSyntaxError('"has_permission_to_url" takes minimal one argument')

    end_tag = 'end' + bits[0]

    kwargs = {}
    for bit in bits[2:]:
        match = kwarg_re.match(bit)
        if not match:
            raise TemplateSyntaxError('Malformed arguments to has_permission_to_url tag')
        name, value = match.groups()
        if name:
            kwargs[name] = parser.compile_filter(value)
        else:
            raise TemplateSyntaxError('Malformed arguments to has_permission_to_url tag')

    nodelist_true = parser.parse(('else', end_tag))
    token = parser.next_token()
    if token.contents == 'else':
        nodelist_false = parser.parse((end_tag,))
        parser.delete_first_token()
    else:
        nodelist_false = NodeList()
    return PermissionURLNode(parser.compile_filter(bits[1]), kwargs, nodelist_true, nodelist_false) 
Example #6
Source File: test_basic.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_unclosed_block(self):
        msg = "Unclosed tag on line 1: 'block'. Looking for one of: endblock."
        with self.assertRaisesMessage(TemplateSyntaxError, msg):
            self.engine.render_to_string('template') 
Example #7
Source File: test_basic.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_basic_syntax23(self):
        """
        Treat "moo #} {{ cow" as the variable. Not ideal, but costly to work
        around, so this triggers an error.
        """
        with self.assertRaises(TemplateSyntaxError):
            self.engine.get_template('basic-syntax23') 
Example #8
Source File: test_basic.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_basic_syntax17(self):
        with self.assertRaises(TemplateSyntaxError):
            self.engine.get_template('basic-syntax17') 
Example #9
Source File: test_basic.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_basic_syntax16(self):
        with self.assertRaises(TemplateSyntaxError):
            self.engine.get_template('basic-syntax16') 
Example #10
Source File: test_basic.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_basic_syntax15(self):
        with self.assertRaises(TemplateSyntaxError):
            self.engine.get_template('basic-syntax15') 
Example #11
Source File: test_basic.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_basic_syntax13(self):
        with self.assertRaises(TemplateSyntaxError):
            self.engine.get_template('basic-syntax13') 
Example #12
Source File: test_basic.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_basic_syntax12(self):
        """
        Raise TemplateSyntaxError when trying to access a variable
        beginning with an underscore.
        """
        with self.assertRaises(TemplateSyntaxError):
            self.engine.get_template('basic-syntax12')

    # Raise TemplateSyntaxError when trying to access a variable
    # containing an illegal character. 
Example #13
Source File: test_toolbox.py    From django-sitemessage with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_templatetag_fails_loud(template_render_tag, template_context, settings):

    settings.DEBUG = True

    with pytest.raises(SiteMessageConfigurationError):
        template_render_tag(
            'sitemessage', 'sitemessage_prefs_table from user_prefs',
            template_context({'user_prefs': 'a'}))

    with pytest.raises(TemplateSyntaxError):
        template_render_tag('sitemessage', 'sitemessage_prefs_table user_prefs') 
Example #14
Source File: test_basic.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_basic_syntax07(self):
        """
        Raise TemplateSyntaxError for empty variable tags.
        """
        with self.assertRaisesMessage(TemplateSyntaxError, 'Empty variable tag on line 1'):
            self.engine.get_template('basic-syntax07') 
Example #15
Source File: test_basic.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_basic_syntax06(self):
        """
        A variable may not contain more than one word
        """
        with self.assertRaises(TemplateSyntaxError):
            self.engine.get_template('basic-syntax06') 
Example #16
Source File: testing.py    From open-humans with MIT License 5 votes vote down vote up
def __mod__(self, other):
        if getattr(other, "var") in allowed_undefined_variables:
            return super(InvalidString, self).__mod__(other)

        from django.template.base import TemplateSyntaxError

        raise TemplateSyntaxError(
            'Undefined variable or unknown value for "{}" ({})'.format(other, other.var)
        ) 
Example #17
Source File: wooey_tags.py    From Wooey with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def gravatar(parser, token):
    try:
        tag_name, email, size = token.split_contents()

    except ValueError:
        raise template.TemplateSyntaxError("%r tag requires email and size arguments" % token.contents.split()[0])

    return GravatarUrlNode(email, size) 
Example #18
Source File: debug.py    From luscan-devel with GNU General Public License v2.0 5 votes vote down vote up
def source_error(self, source, msg):
        e = TemplateSyntaxError(msg)
        e.django_template_source = source
        return e 
Example #19
Source File: thumbnails.py    From python-thumbnails with MIT License 5 votes vote down vote up
def render(self, context):
        from thumbnails import get_thumbnail  # imported inline in order for mocking to work
        if self.original and self.size:
            original = self.original.resolve(context)
            size = self.size.resolve(context)
            options = {}
            for key in self.options:
                options[key] = self.options[key].resolve(context)

            context[self.variable_name] = get_thumbnail(original, size, **options)
        else:
            raise TemplateSyntaxError()

        return '' 
Example #20
Source File: settings.py    From opensurfaces with MIT License 5 votes vote down vote up
def __mod__(self, other):
            from django.template.base import TemplateSyntaxError
            raise TemplateSyntaxError(
                "Undefined variable or unknown value for: %s" % repr(other)) 
Example #21
Source File: wooey_tags.py    From Wooey with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def simple_assignment_tag(self, func=None, takes_context=None, name=None):
            '''
            Like assignment_tag but when "as" not provided, falls back to simple_tag behavior!
            NOTE: this is based on Django's assignment_tag implementation, modified as needed.

            https://gist.github.com/natevw/f14812604be62c073461
            '''
            # (nvw) imports necessary to match original context

            def dec(func):
                params, varargs, varkw, defaults = getargspec(func)

                # (nvw) added from Django's simple_tag implementation
                class SimpleNode(TagHelperNode):
                    def render(self, context):
                        resolved_args, resolved_kwargs = self.get_resolved_arguments(context)
                        return func(*resolved_args, **resolved_kwargs)

                class AssignmentNode(TagHelperNode):
                    def __init__(self, takes_context, args, kwargs, target_var):
                        super(AssignmentNode, self).__init__(takes_context, args, kwargs)
                        self.target_var = target_var

                    def render(self, context):
                        resolved_args, resolved_kwargs = self.get_resolved_arguments(context)
                        context[self.target_var] = func(*resolved_args, **resolved_kwargs)
                        return ''

                function_name = (name or
                    getattr(func, '_decorated_function', func).__name__)

                def compile_func(parser, token):
                    bits = token.split_contents()[1:]
                    if len(bits) > 2 and bits[-2] == 'as':
                        target_var = bits[-1]
                        bits = bits[:-2]
                        args, kwargs = parse_bits(parser, bits, params,
                            varargs, varkw, defaults, takes_context, function_name)
                        return AssignmentNode(takes_context, args, kwargs, target_var)
                    else:
                        args, kwargs = parse_bits(parser, bits, params,
                            varargs, varkw, defaults, takes_context, function_name)
                        return SimpleNode(takes_context, args, kwargs)

                compile_func.__doc__ = func.__doc__
                self.tag(function_name, compile_func)
                return func

            if func is None:
                # @register.assignment_tag(...)
                return dec
            elif callable(func):
                # @register.assignment_tag
                return dec(func)
            else:
                raise TemplateSyntaxError("Invalid arguments provided to assignment_tag")