Python django.template.VariableDoesNotExist() Examples

The following are 30 code examples for showing how to use django.template.VariableDoesNotExist(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.template , or try the search function .

Example 1
Project: jbox   Author: jpush   File: wtforms.py    License: MIT License 6 votes vote down vote up
def render(self, context):
        try:
            if '.' in self.field_var:
                base, field_name = self.field_var.rsplit('.', 1)
                field = getattr(Variable(base).resolve(context), field_name)
            else:
                field = context[self.field_var]
        except (template.VariableDoesNotExist, KeyError, AttributeError):
            return settings.TEMPLATE_STRING_IF_INVALID

        h_attrs = {}
        for k, v in iteritems(self.html_attrs):
            try:
                h_attrs[k] = v.resolve(context)
            except template.VariableDoesNotExist:
                h_attrs[k] = settings.TEMPLATE_STRING_IF_INVALID

        return field(**h_attrs) 
Example 2
Project: RSSNewsGAE   Author: liantian-cn   File: wtforms.py    License: Apache License 2.0 6 votes vote down vote up
def render(self, context):
        try:
            if '.' in self.field_var:
                base, field_name = self.field_var.rsplit('.', 1)
                field = getattr(Variable(base).resolve(context), field_name)
            else:
                field = context[self.field_var]
        except (template.VariableDoesNotExist, KeyError, AttributeError):
            return settings.TEMPLATE_STRING_IF_INVALID

        h_attrs = {}
        for k, v in iteritems(self.html_attrs):
            try:
                h_attrs[k] = v.resolve(context)
            except template.VariableDoesNotExist:
                h_attrs[k] = settings.TEMPLATE_STRING_IF_INVALID

        return field(**h_attrs) 
Example 3
Project: googleapps-message-recall   Author: google   File: wtforms.py    License: Apache License 2.0 6 votes vote down vote up
def render(self, context):
        try:
            if '.' in self.field_var:
                base, field_name = self.field_var.rsplit('.', 1)
                field = getattr(Variable(base).resolve(context), field_name)
            else:
                field = context[self.field_var]
        except (template.VariableDoesNotExist, KeyError, AttributeError):
            return settings.TEMPLATE_STRING_IF_INVALID

        h_attrs = {}
        for k, v in iteritems(self.html_attrs):
            try:
                h_attrs[k] = v.resolve(context)
            except template.VariableDoesNotExist:
                h_attrs[k] = settings.TEMPLATE_STRING_IF_INVALID

        return field(**h_attrs) 
Example 4
Project: wagtail   Author: wagtail   File: wagtailcore_tags.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def render(self, context):
        try:
            value = self.block_var.resolve(context)
        except template.VariableDoesNotExist:
            return ''

        if hasattr(value, 'render_as_block'):
            if self.use_parent_context:
                new_context = context.flatten()
            else:
                new_context = {}

            if self.extra_context:
                for var_name, var_value in self.extra_context.items():
                    new_context[var_name] = var_value.resolve(context)

            return value.render_as_block(context=new_context)
        else:
            return force_str(value) 
Example 5
Project: django-hyper-editor   Author: DivineITLimited   File: hyper_tags.py    License: MIT License 6 votes vote down vote up
def render(self, context):
        try:
            value = self.block_var.resolve(context)
        except template.VariableDoesNotExist:
            return ''

        if isinstance(value, dict): # its an preview
            code_renderer = CodeRenderer()
            bl_class = get_block_class_for(value.get('type', 'INVALID_PLUGIN_WITH_NO_TYPE'))
            if bl_class:
                instance = bl_class(value, code_renderer)
                return instance.render(context)
            else:
                return ''
        elif isinstance(value, str):
            try:
                value = HyperFieldResponse.from_str(value)
                return value.render(context)
            except Exception as e:
                print(e)
                return ''
        else:
            return value.render(context) 
Example 6
Project: luscan-devel   Author: blackye   File: cache.py    License: GNU General Public License v2.0 6 votes vote down vote up
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)
        # Build a key for this fragment and all vary-on's.
        key = ':'.join([urlquote(resolve_variable(var, context)) for var in self.vary_on])
        args = hashlib.md5(force_bytes(key))
        cache_key = 'template.cache.%s.%s' % (self.fragment_name, args.hexdigest())
        value = cache.get(cache_key)
        if value is None:
            value = self.nodelist.render(context)
            cache.set(cache_key, value, expire_time)
        return value 
Example 7
Project: django-userena-ce   Author: django-userena-ce   File: umessages_tags.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def render(self, context):
        try:
            user = self.user.resolve(context)
        except template.VariableDoesNotExist:
            return ""

        if not self.um_to_user:
            message_count = MessageRecipient.objects.count_unread_messages_for(user)

        else:
            try:
                um_to_user = self.um_to_user.resolve(context)
            except template.VariableDoesNotExist:
                return ""

            message_count = MessageRecipient.objects.count_unread_messages_between(
                user, um_to_user
            )

        context[self.var_name] = message_count

        return "" 
Example 8
Project: Django-2-Web-Development-Cookbook-Third-Edition   Author: PacktPublishing   File: utility_tags.py    License: MIT License 6 votes vote down vote up
def render(self, context):
        if "." in self.manager_method:
            manager, method = self.manager_method.split(".")
        else:
            manager = "_default_manager"
            method = self.manager_method

        model_manager = getattr(self.model, manager)
        fallback_method = self.model._default_manager.none
        qs = getattr(model_manager, method, fallback_method)()
        limit = None
        if self.limit:
            try:
                limit = self.limit.resolve(context)
            except template.VariableDoesNotExist:
                limit = None
        context[self.var_name] = qs[:limit] if limit else qs
        return "" 
Example 9
Project: Django-2-Web-Development-Cookbook-Third-Edition   Author: PacktPublishing   File: utility_tags.py    License: MIT License 6 votes vote down vote up
def render(self, context):
        if "." in self.manager_method:
            manager, method = self.manager_method.split(".")
        else:
            manager = "_default_manager"
            method = self.manager_method

        model_manager = getattr(self.model, manager)
        fallback_method = self.model._default_manager.none
        qs = getattr(model_manager, method, fallback_method)()
        limit = None
        if self.limit:
            try:
                limit = self.limit.resolve(context)
            except template.VariableDoesNotExist:
                limit = None
        context[self.var_name] = qs[:limit] if limit else qs
        return "" 
Example 10
Project: Django-2-Web-Development-Cookbook-Third-Edition   Author: PacktPublishing   File: utility_tags.py    License: MIT License 6 votes vote down vote up
def render(self, context):
        if "." in self.manager_method:
            manager, method = self.manager_method.split(".")
        else:
            manager = "_default_manager"
            method = self.manager_method

        model_manager = getattr(self.model, manager)
        fallback_method = self.model._default_manager.none
        qs = getattr(model_manager, method, fallback_method)()
        limit = None
        if self.limit:
            try:
                limit = self.limit.resolve(context)
            except template.VariableDoesNotExist:
                limit = None
        context[self.var_name] = qs[:limit] if limit else qs
        return "" 
Example 11
Project: Django-2-Web-Development-Cookbook-Third-Edition   Author: PacktPublishing   File: utility_tags.py    License: MIT License 6 votes vote down vote up
def render(self, context):
        if "." in self.manager_method:
            manager, method = self.manager_method.split(".")
        else:
            manager = "_default_manager"
            method = self.manager_method

        model_manager = getattr(self.model, manager)
        fallback_method = self.model._default_manager.none
        qs = getattr(model_manager, method, fallback_method)()
        limit = None
        if self.limit:
            try:
                limit = self.limit.resolve(context)
            except template.VariableDoesNotExist:
                limit = None
        context[self.var_name] = qs[:limit] if limit else qs
        return "" 
Example 12
Project: Django-2-Web-Development-Cookbook-Third-Edition   Author: PacktPublishing   File: utility_tags.py    License: MIT License 6 votes vote down vote up
def render(self, context):
        if "." in self.manager_method:
            manager, method = self.manager_method.split(".")
        else:
            manager = "_default_manager"
            method = self.manager_method

        model_manager = getattr(self.model, manager)
        fallback_method = self.model._default_manager.none
        qs = getattr(model_manager, method, fallback_method)()
        limit = None
        if self.limit:
            try:
                limit = self.limit.resolve(context)
            except template.VariableDoesNotExist:
                limit = None
        context[self.var_name] = qs[:limit] if limit else qs
        return "" 
Example 13
Project: Django-2-Web-Development-Cookbook-Third-Edition   Author: PacktPublishing   File: utility_tags.py    License: MIT License 6 votes vote down vote up
def render(self, context):
        if "." in self.manager_method:
            manager, method = self.manager_method.split(".")
        else:
            manager = "_default_manager"
            method = self.manager_method

        model_manager = getattr(self.model, manager)
        fallback_method = self.model._default_manager.none
        qs = getattr(model_manager, method, fallback_method)()
        limit = None
        if self.limit:
            try:
                limit = self.limit.resolve(context)
            except template.VariableDoesNotExist:
                limit = None
        context[self.var_name] = qs[:limit] if limit else qs
        return "" 
Example 14
Project: Django-2-Web-Development-Cookbook-Third-Edition   Author: PacktPublishing   File: utility_tags.py    License: MIT License 6 votes vote down vote up
def render(self, context):
        if "." in self.manager_method:
            manager, method = self.manager_method.split(".")
        else:
            manager = "_default_manager"
            method = self.manager_method

        model_manager = getattr(self.model, manager)
        fallback_method = self.model._default_manager.none
        qs = getattr(model_manager, method, fallback_method)()
        limit = None
        if self.limit:
            try:
                limit = self.limit.resolve(context)
            except template.VariableDoesNotExist:
                limit = None
        context[self.var_name] = qs[:limit] if limit else qs
        return "" 
Example 15
Project: Django-2-Web-Development-Cookbook-Third-Edition   Author: PacktPublishing   File: utility_tags.py    License: MIT License 6 votes vote down vote up
def render(self, context):
        if "." in self.manager_method:
            manager, method = self.manager_method.split(".")
        else:
            manager = "_default_manager"
            method = self.manager_method

        model_manager = getattr(self.model, manager)
        fallback_method = self.model._default_manager.none
        qs = getattr(model_manager, method, fallback_method)()
        limit = None
        if self.limit:
            try:
                limit = self.limit.resolve(context)
            except template.VariableDoesNotExist:
                limit = None
        context[self.var_name] = qs[:limit] if limit else qs
        return "" 
Example 16
Project: djongo   Author: nesdis   File: test_logging.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def test_log_on_variable_does_not_exist_not_silent(self):
        with self.assertLogs('django.template', self.loglevel) as cm:
            with self.assertRaises(VariableDoesNotExist):
                Variable('article.author').resolve({'article': {'section': 'News'}})

        self.assertEqual(len(cm.records), 1)
        log_record = cm.records[0]
        self.assertEqual(
            log_record.getMessage(),
            "Exception while resolving variable 'author' in template 'unknown'."
        )
        self.assertIsNotNone(log_record.exc_info)
        raised_exception = log_record.exc_info[1]
        self.assertEqual(
            str(raised_exception),
            "Failed lookup for key [author] in {'section': 'News'}"
        ) 
Example 17
Project: Django-3-Web-Development-Cookbook-Fourth-Edition   Author: PacktPublishing   File: utility_tags.py    License: MIT License 6 votes vote down vote up
def render(self, context):
        if "." in self.manager_method:
            manager, method = self.manager_method.split(".")
        else:
            manager = "_default_manager"
            method = self.manager_method

        model_manager = getattr(self.model, manager)
        fallback_method = self.model._default_manager.none
        qs = getattr(model_manager, method, fallback_method)()
        limit = None
        if self.limit:
            try:
                limit = self.limit.resolve(context)
            except template.VariableDoesNotExist:
                limit = None
        context[self.var_name] = qs[:limit] if limit else qs
        return "" 
Example 18
Project: Django-3-Web-Development-Cookbook-Fourth-Edition   Author: PacktPublishing   File: utility_tags.py    License: MIT License 6 votes vote down vote up
def render(self, context):
        if "." in self.manager_method:
            manager, method = self.manager_method.split(".")
        else:
            manager = "_default_manager"
            method = self.manager_method

        model_manager = getattr(self.model, manager)
        fallback_method = self.model._default_manager.none
        qs = getattr(model_manager, method, fallback_method)()
        limit = None
        if self.limit:
            try:
                limit = self.limit.resolve(context)
            except template.VariableDoesNotExist:
                limit = None
        context[self.var_name] = qs[:limit] if limit else qs
        return "" 
Example 19
Project: Django-3-Web-Development-Cookbook-Fourth-Edition   Author: PacktPublishing   File: utility_tags.py    License: MIT License 6 votes vote down vote up
def render(self, context):
        if "." in self.manager_method:
            manager, method = self.manager_method.split(".")
        else:
            manager = "_default_manager"
            method = self.manager_method

        model_manager = getattr(self.model, manager)
        fallback_method = self.model._default_manager.none
        qs = getattr(model_manager, method, fallback_method)()
        limit = None
        if self.limit:
            try:
                limit = self.limit.resolve(context)
            except template.VariableDoesNotExist:
                limit = None
        context[self.var_name] = qs[:limit] if limit else qs
        return "" 
Example 20
Project: Django-3-Web-Development-Cookbook-Fourth-Edition   Author: PacktPublishing   File: utility_tags.py    License: MIT License 6 votes vote down vote up
def render(self, context):
        if "." in self.manager_method:
            manager, method = self.manager_method.split(".")
        else:
            manager = "_default_manager"
            method = self.manager_method

        model_manager = getattr(self.model, manager)
        fallback_method = self.model._default_manager.none
        qs = getattr(model_manager, method, fallback_method)()
        limit = None
        if self.limit:
            try:
                limit = self.limit.resolve(context)
            except template.VariableDoesNotExist:
                limit = None
        context[self.var_name] = qs[:limit] if limit else qs
        return "" 
Example 21
Project: Django-3-Web-Development-Cookbook-Fourth-Edition   Author: PacktPublishing   File: utility_tags.py    License: MIT License 6 votes vote down vote up
def render(self, context):
        if "." in self.manager_method:
            manager, method = self.manager_method.split(".")
        else:
            manager = "_default_manager"
            method = self.manager_method

        model_manager = getattr(self.model, manager)
        fallback_method = self.model._default_manager.none
        qs = getattr(model_manager, method, fallback_method)()
        limit = None
        if self.limit:
            try:
                limit = self.limit.resolve(context)
            except template.VariableDoesNotExist:
                limit = None
        context[self.var_name] = qs[:limit] if limit else qs
        return "" 
Example 22
Project: Django-3-Web-Development-Cookbook-Fourth-Edition   Author: PacktPublishing   File: utility_tags.py    License: MIT License 6 votes vote down vote up
def render(self, context):
        if "." in self.manager_method:
            manager, method = self.manager_method.split(".")
        else:
            manager = "_default_manager"
            method = self.manager_method

        model_manager = getattr(self.model, manager)
        fallback_method = self.model._default_manager.none
        qs = getattr(model_manager, method, fallback_method)()
        limit = None
        if self.limit:
            try:
                limit = self.limit.resolve(context)
            except template.VariableDoesNotExist:
                limit = None
        context[self.var_name] = qs[:limit] if limit else qs
        return "" 
Example 23
Project: Django-3-Web-Development-Cookbook-Fourth-Edition   Author: PacktPublishing   File: utility_tags.py    License: MIT License 6 votes vote down vote up
def render(self, context):
        if "." in self.manager_method:
            manager, method = self.manager_method.split(".")
        else:
            manager = "_default_manager"
            method = self.manager_method

        model_manager = getattr(self.model, manager)
        fallback_method = self.model._default_manager.none
        qs = getattr(model_manager, method, fallback_method)()
        limit = None
        if self.limit:
            try:
                limit = self.limit.resolve(context)
            except template.VariableDoesNotExist:
                limit = None
        context[self.var_name] = qs[:limit] if limit else qs
        return "" 
Example 24
Project: steemprojects.com   Author: wise-team   File: grid_tags.py    License: MIT License 6 votes vote down vote up
def style_attribute(attribute_name, package):
    mappings = {
            'title': style_title,
            'repo_description': style_repo_description,
            'commits_over_52': style_commits,
    }

    as_var = template.Variable('package.' + attribute_name)
    try:
        value = as_var.resolve({'package': package})
    except template.VariableDoesNotExist:
        value = ''

    if attribute_name in list(mappings.keys()):
        return  mappings[attribute_name](value)

    return style_default(value) 
Example 25
Project: rocket-league-replays   Author: rocket-league-replays   File: news.py    License: GNU General Public License v3.0 6 votes vote down vote up
def takes_current_page(func):
    """Decorator for template tags that require the current page."""

    @wraps(func)
    def do_takes_current_page(context, *args, **kwargs):
        page = get_page_from_context(context, kwargs)

        if not page:
            page = get_default_news_page()
        if page is None:
            raise template.VariableDoesNotExist("Could not determine the current page from the template context.")
        kwargs["page"] = page

        return func(context, *args, **kwargs)

    return do_takes_current_page 
Example 26
Project: donation-tracker   Author: GamesDoneQuick   File: donation_tags.py    License: Apache License 2.0 5 votes vote down vote up
def tryresolve(var, context, default=None):
    try:
        return var.resolve(context)
    except template.VariableDoesNotExist:
        return default 
Example 27
Project: donation-tracker   Author: GamesDoneQuick   File: donation_tags.py    License: Apache License 2.0 5 votes vote down vote up
def render(self, context):
        try:
            time = self.time.resolve(context)
            try:
                now = datetime.datetime.now() - time
            except TypeError:
                return ''
            return '%d.%d seconds' % (now.seconds, now.microseconds)
        except template.VariableDoesNotExist:
            return '' 
Example 28
Project: coursys   Author: sfu-fas   File: course_display.py    License: GNU General Public License v3.0 5 votes vote down vote up
def hash(object, attr):
    pseudo_context = { 'object' : object }
    try:
        value = Variable('object.%s' % attr).resolve(pseudo_context)
    except VariableDoesNotExist:
        value = None
    return value 
Example 29
Project: coursys   Author: sfu-fas   File: grade_student.py    License: GNU General Public License v3.0 5 votes vote down vote up
def render(self, context):
        try:
            dictionary = self.dictionary.resolve(context)
            aslug = self.aslug.resolve(context)
            userid = self.userid.resolve(context)
        except template.VariableDoesNotExist:
            return "?"

        grades = dictionary[aslug]
        if userid not in grades:
            return ''

        grade = grades[userid]
        gtext = escape(grade.display_staff_short())

        stext = ''
        if grade.flag not in ['GRAD', 'NOGR', 'CALC']:
            stext = '(' + grade.get_flag_display() + ')'


        if grade.comment:
            ctext = escape(grade.comment)
            stext = '(' + grade.get_flag_display() + ')'
        else:
            ctext = ''
        
        if ctext and stext:
            return mark_safe(gtext + '<span class="more"><span title="' + ctext + '"><img src="/media/icons/information.png" alt="[I]" /></span><br/>' + stext + '</span>')
        elif stext:
            return mark_safe(gtext + '<span class="more"><br/>' + stext + '</span>')
        else:
            return mark_safe(gtext) 
Example 30
Project: templated-docs   Author: alexmorozov   File: templated_docs_tags.py    License: MIT License 5 votes vote down vote up
def render(self, context):
        try:
            self.value = self.value.resolve(context)
            if not isinstance(self.value, ImageFieldFile):
                raise template.VariableDoesNotExist(
                    'Image argument should be an ImageField')
            images = context.dicts[0].setdefault('ootemplate_imgs', {})

            id = len(images)
            z_index = id + 3  # Magic
            width = self.value.width * PIXEL_TO_CM
            height = self.value.height * PIXEL_TO_CM
            filename = os.path.basename(self.value.name)
            basename = os.path.splitext(filename)[0]

            images[self.value.path] = self.value
            return ('<draw:frame draw:style-name="gr%(z_index)s" '
                    'draw:name="%(basename)s" '
                    'draw:id="id%(id)s" '
                    'text:anchor-type="char" svg:width="%(width)fcm" '
                    'svg:height="%(height)fcm" draw:z-index="%(z_index)s">'
                    '<draw:image xlink:href="Pictures/%(filename)s" '
                    'xlink:type="simple" xlink:show="embed" '
                    'xlink:actuate="onLoad"/></draw:frame>') % locals()
        except template.VariableDoesNotExist:
            return ''