django.template.Template - python examples

Here are the examples of the python api django.template.Template 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 : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_result_list_empty_changelist_value(self):
        """
        Regression test for #14982: EMPTY_CHANGELIST_VALUE should be honored
        for relationship fields
        """
        new_child = Child.objects.create(name='name', parent=None)
        request = self.factory.get('/child/')
        request.user = self.superuser
        m = ChildAdmin(Child, custom_site)
        cl = m.get_changelist_instance(request)
        cl.formset = None
        template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
        context = Context({'cl': cl, 'opts': Child._meta})
        table_output = template.render(context)
        link = reverse('admin:admin_changelist_child_change', args=(new_child.id,))
        row_html = build_tbody_html(new_child.id, link, '<td clast="field-parent nowrap">-</td>')
        self.astertNotEqual(table_output.find(row_html), -1, 'Failed to find expected row element: %s' % table_output)

3 View Complete Implementation : test_blocktrans.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_single_locale_activation(self):
        """
        Simple baseline behavior with one locale for all the supported i18n
        constructs.
        """
        with translation.override('fr'):
            self.astertEqual(
                Template("{% load i18n %}{% blocktrans %}Yes{% endblocktrans %}").render(Context({})),
                'Oui'
            )

3 View Complete Implementation : test_percents.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_translates_with_a_percent_symbol_at_the_end(self):
        expected = 'Littérale avec un symbole de pour cent à la fin %'

        trans_tpl = Template('{% load i18n %}{% trans "Literal with a percent symbol at the end %" %}')
        self.astertEqual(trans_tpl.render(Context({})), expected)

        block_tpl = Template(
            '{% load i18n %}{% blocktrans %}Literal with a percent symbol at '
            'the end %{% endblocktrans %}'
        )
        self.astertEqual(block_tpl.render(Context({})), expected)

3 View Complete Implementation : test_blocktrans.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    @override_settings(LOCALE_PATHS=[os.path.join(here, 'other', 'locale')])
    def test_bad_placeholder_2(self):
        """
        Error in translation file should not crash template rendering (#18393).
        (%(person) misses a 's' in fr.po, causing the string formatting to fail)
        .
        """
        with translation.override('fr'):
            t = Template('{% load i18n %}{% blocktrans %}My other name is {{ person }}.{% endblocktrans %}')
            rendered = t.render(Context({'person': 'James'}))
            self.astertEqual(rendered, 'My other name is James.')

3 View Complete Implementation : test_modelchoicefield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_num_queries(self):
        """
        Widgets that render multiple subwidgets shouldn't make more than one
        database query.
        """
        categories = Category.objects.all()

        clast CategoriesForm(forms.Form):
            radio = forms.ModelChoiceField(queryset=categories, widget=forms.RadioSelect)
            checkbox = forms.ModelMultipleChoiceField(queryset=categories, widget=forms.CheckboxSelectMultiple)

        template = Template(
            '{% for widget in form.checkbox %}{{ widget }}{% endfor %}'
            '{% for widget in form.radio %}{{ widget }}{% endfor %}'
        )
        with self.astertNumQueries(2):
            template.render(Context({'form': CategoriesForm()}))

3 View Complete Implementation : views.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
def raw_post_view(request):
    """A view which expects raw XML to be posted and returns content extracted
    from the XML"""
    if request.method == 'POST':
        root = parseString(request.body)
        first_book = root.firstChild.firstChild
        satle, author = [n.firstChild.nodeValue for n in first_book.childNodes]
        t = Template("{{ satle }} - {{ author }}", name="Book template")
        c = Context({"satle": satle, "author": author})
    else:
        t = Template("GET request.", name="Book GET template")
        c = Context()

    return HttpResponse(t.render(c))

3 View Complete Implementation : views.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
def put_view(request):
    if request.method == 'PUT':
        t = Template('Data received: {{ data }} is the body.', name='PUT Template')
        c = Context({
            'Content-Length': request.META['CONTENT_LENGTH'],
            'data': request.body.decode(),
        })
    else:
        t = Template('Viewing GET page.', name='Empty GET Template')
        c = Context()
    return HttpResponse(t.render(c))

3 View Complete Implementation : views.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
def formset_view(request):
    "A view that tests a simple formset"
    if request.method == 'POST':
        formset = TestFormSet(request.POST)
        if formset.is_valid():
            t = Template('Valid POST data.', name='Valid POST Template')
            c = Context()
        else:
            t = Template('Invalid POST data. {{ my_formset.errors }}',
                         name='Invalid POST Template')
            c = Context({'my_formset': formset})
    else:
        formset = TestForm(request.GET)
        t = Template('Viewing base formset. {{ my_formset }}.',
                     name='Formset GET Template')
        c = Context({'my_formset': formset})
    return HttpResponse(t.render(c))

3 View Complete Implementation : test_blocktrans.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    @override_settings(LOCALE_PATHS=[os.path.join(here, 'other', 'locale')])
    def test_bad_placeholder_1(self):
        """
        Error in translation file should not crash template rendering (#16516).
        (%(person)s is translated as %(personne)s in fr.po).
        """
        with translation.override('fr'):
            t = Template('{% load i18n %}{% blocktrans %}My name is {{ person }}.{% endblocktrans %}')
            rendered = t.render(Context({'person': 'James'}))
            self.astertEqual(rendered, 'My name is James.')

3 View Complete Implementation : views.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
def json_view(request):
    """
    A view that expects a request with the header 'application/json' and JSON
    data with a key named 'value'.
    """
    if request.META.get('CONTENT_TYPE') != 'application/json':
        return HttpResponse()

    t = Template('Viewing {} page. With data {{ data }}.'.format(request.method))
    data = json.loads(request.body.decode('utf-8'))
    c = Context({'data': data['value']})
    return HttpResponse(t.render(c))