Ticket #36872: proposal_2.diff

File proposal_2.diff, 3.4 KB (added by Ricardo Robles, 3 weeks ago)
  • AUTHORS

    diff --git a/AUTHORS b/AUTHORS
    index f6d4c5f..783f50b 100644
    a b answer newbie questions, and generally made Django that much better:  
    896896    rhettg@gmail.com
    897897    Ricardo Javier Cárdenes Medina <ricardo.cardenes@gmail.com>
    898898    ricardojbarrios@gmail.com
     899    Ricardo Robles Fernández <ricardo.r.f@hotmail.com>
    899900    Riccardo Di Virgilio
    900901    Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
    901902    Richard Davies <richard.davies@elastichosts.com>
  • django/template/base.py

    diff --git a/django/template/base.py b/django/template/base.py
    index d6595e3..d2cd89c 100644
    a b times with multiple contexts)  
    5050'<html></html>'
    5151"""
    5252
     53import asyncio
    5354import inspect
    5455import logging
    5556import re
    5657import warnings
    5758from enum import Enum
    5859
     60from asgiref.sync import async_to_sync
     61
    5962from django.template.context import BaseContext
    6063from django.utils.deprecation import django_file_prefixes
    6164from django.utils.formats import localize
    class FilterExpression:  
    786789        if self.is_var:
    787790            try:
    788791                obj = self.var.resolve(context)
     792                if asyncio.iscoroutine(obj):
     793                    obj = async_to_sync(lambda: obj)()
     794
    789795            except VariableDoesNotExist:
    790796                if ignore_failures:
    791797                    obj = None
  • docs/ref/templates/language.txt

    diff --git a/docs/ref/templates/language.txt b/docs/ref/templates/language.txt
    index d80c2f6..f76f4f1 100644
    a b Use a dot (``.``) to access attributes of a variable.  
    9494    * Attribute or method lookup
    9595    * Numeric index lookup
    9696
    97     If the resulting value is callable, it is called with no arguments. The
    98     result of the call becomes the template value.
     97    If the resulting value is callable, it is called with no arguments; if it
     98    is a coroutine, it is awaited using ``async_to_sync()``. The result of
     99    the call becomes the template value.
    99100
    100101    This lookup order can cause some unexpected behavior with objects that
    101102    override dictionary lookup. For example, consider the following code
  • tests/template_tests/test_base.py

    diff --git a/tests/template_tests/test_base.py b/tests/template_tests/test_base.py
    index 33a200c..3cd594e 100644
    a b class TemplateTests(SimpleTestCase):  
    6464        )
    6565
    6666
     67class AsyncTemplateTests(SimpleTestCase):
     68    def test_async_property_resolution(self):
     69        from django.template import engines
     70
     71        django_engine = engines["django"]
     72
     73        class Example:
     74            def sync_method(self):
     75                return "Synchronous Method Result"
     76
     77            async def async_method(self):
     78                return "Asynchronous Method Result"
     79
     80        html_string = """
     81        <!DOCTYPE html>
     82        <html>
     83        <body>
     84            <p>sync: {{ example.sync_method }}</p>
     85            <p>async: {{ example.async_method }}</p>
     86        </body>
     87        </html>
     88        """
     89
     90        template = django_engine.from_string(html_string)
     91        rendered_html = template.render({"example": Example()})
     92
     93        self.assertIn("sync: Synchronous Method Result", rendered_html)
     94        self.assertIn("async: Asynchronous Method Result", rendered_html)
     95
     96
    6797class VariableDoesNotExistTests(SimpleTestCase):
    6898    def test_str(self):
    6999        exc = VariableDoesNotExist(msg="Failed lookup in %r", params=({"foo": "bar"},))
Back to Top