Skip to content

Commit 0cfe69e

Browse files
committed
Merge remote-tracking branch 'origin/main' into correct_type_render_block_to_string
2 parents ab81cf7 + ae6890d commit 0cfe69e

File tree

8 files changed

+126
-22
lines changed

8 files changed

+126
-22
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545

4646
strategy:
4747
matrix:
48-
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
48+
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
4949

5050
steps:
5151
- uses: actions/checkout@v2

CHANGELOG.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,24 @@
33
Changelog
44
#########
55

6+
next
7+
====
8+
9+
Improvements
10+
------------
11+
12+
* Add a new ``render_block`` function which returns a `HttpResponse` with the content
13+
set to the result of calling ``render_block_to_string()``. Contributed by
14+
`@gogognome <https://github.com/gogognome>`_. (`#60 <https://github.com/clokep/django-render-block/pull/60>`_)
15+
16+
Maintenance
17+
-----------
18+
19+
* Support Python 3.13. (`#62 <https://github.com/clokep/django-render-block/pull/62>`_)
20+
* Drop support for Python 3.8. (`#62 <https://github.com/clokep/django-render-block/pull/62>`_)
21+
* Support Django 5.2. (`#62 <https://github.com/clokep/django-render-block/pull/62>`_)
22+
* Drop support for Django 5.0. (`#62 <https://github.com/clokep/django-render-block/pull/62>`_)
23+
624
0.10 (July 15, 2024)
725
====================
826

README.rst

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ Features
2323
Requirements
2424
============
2525

26-
Django Render Block supports Django 4.2, 5.0, and 5.1 on Python 3.8, 3.9, 3.10,
27-
3.11 and 3.12 (see the Django documentation for which versions of Python are
28-
supported by particular Django versions).
26+
Django Render Block supports Django 4.2, 5.1, and 5.2 on Python 3.9, 3.10, 3.11,
27+
3.12, and 3.13 (see the `Django documentation <https://docs.djangoproject.com/en/dev/faq/install/#what-python-version-can-i-use-with-django>`_
28+
for which versions of Python are supported by particular Django versions).
2929

3030
Examples
3131
========
@@ -71,7 +71,7 @@ And from Python:
7171
API Reference
7272
=============
7373

74-
The API is simple and attempts to mirror the built-in ``render_to_string`` API.
74+
The API is simple and attempts to mirror the built-in ``render_to_string`` and ``render`` API.
7575

7676
``render_block_to_string(template_name, block_name, context=None, request=None)``
7777

@@ -95,6 +95,34 @@ The API is simple and attempts to mirror the built-in ``render_to_string`` API.
9595
``request`` is optional and works only for Django templates. If both context and request
9696
are provided, a ``RequestContext`` will be used instead of a ``Context``.
9797

98+
Similarly there is a ``render_block`` function which returns an `HttpResponse` with
99+
the content sent to the result of ``render_block_to_string`` with the same parameters.
100+
101+
``render_block(request, template_name, block_name, context=None, content_type="text/html", status=200)``
102+
103+
``request``
104+
The request object used to render the template.
105+
106+
``template_name``
107+
The name of the template to load and render. If it’s a list of template
108+
names, Django uses ``select_template()`` instead of ``get_template()``
109+
to find the template.
110+
111+
``block_name``
112+
The name of the block to render from the above template.
113+
114+
``context``
115+
A ``dict`` to be used as the template’s context for rendering. A ``Context``
116+
object can be provided for Django templates.
117+
118+
``context`` is optional. If not provided, an empty context will be used.
119+
120+
``content_type``
121+
A ``str`` content type for the HTTP response.
122+
123+
``status``
124+
An ``int`` HTTP status code for the HTTP response.
125+
98126
Exceptions
99127
----------
100128

render_block/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
from render_block.base import render_block_to_string
1+
from render_block.base import render_block, render_block_to_string
22
from render_block.exceptions import BlockNotFound, UnsupportedEngine
33

4-
__all__ = ["BlockNotFound", "UnsupportedEngine", "render_block_to_string"]
4+
__all__ = [
5+
"BlockNotFound",
6+
"UnsupportedEngine",
7+
"render_block",
8+
"render_block_to_string",
9+
]

render_block/base.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import List, Optional, Tuple, Union
22

3-
from django.http import HttpRequest
3+
from django.http import HttpRequest, HttpResponse
44
from django.template import Context, loader
55
from django.template.backends.django import Template as DjangoTemplate
66

@@ -27,10 +27,13 @@ def render_block_to_string(
2727
Loads the given template_name and renders the given block with the given
2828
dictionary as context. Returns a string.
2929
30-
template_name
30+
:param template_name:
3131
The name of the template to load and render. If it's a list of
3232
template names, Django uses select_template() instead of
3333
get_template() to find the template.
34+
:param block_name: The name of the block to load.
35+
:param context: The context dictionary used while rendering the template.
36+
:param request: The request that triggers the rendering of the block.
3437
"""
3538

3639
# Like render_to_string, template_name can be a string or a list/tuple.
@@ -55,3 +58,19 @@ def render_block_to_string(
5558
raise UnsupportedEngine(
5659
"Can only render blocks from the Django template backend."
5760
)
61+
62+
63+
def render_block(
64+
request: HttpRequest,
65+
template_name: str,
66+
block_name: str,
67+
context: Optional[Context] = None,
68+
content_type: Optional[str] = None,
69+
status: Optional[int] = None,
70+
) -> HttpResponse:
71+
"""
72+
Return an HttpResponse whose content is filled with the result of calling
73+
render_block.render_block_to_string() with the passed arguments.
74+
"""
75+
content = render_block_to_string(template_name, block_name, context, request)
76+
return HttpResponse(content, content_type, status)

setup.cfg

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ classifiers =
1717
Environment :: Web Environment
1818
Topic :: Internet
1919
Framework :: Django
20-
Framework :: Django :: 3.2
21-
Framework :: Django :: 4.1
20+
Framework :: Django :: 4.2
21+
Framework :: Django :: 5.1
22+
Framework :: Django :: 5.2
2223
Programming Language :: Python
23-
Programming Language :: Python :: 3.8
2424
Programming Language :: Python :: 3.9
2525
Programming Language :: Python :: 3.10
2626
Programming Language :: Python :: 3.11
2727
Programming Language :: Python :: 3.12
28+
Programming Language :: Python :: 3.13
2829
License :: OSI Approved :: ISC License (ISCL)
2930
project_urls =
3031
Documentation = https://github.com/clokep/django-render-block/blob/main/README.rst

tests/tests.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
from django.template import Context
44
from django.test import RequestFactory, TestCase, modify_settings, override_settings
55

6-
from render_block import BlockNotFound, UnsupportedEngine, render_block_to_string
6+
from render_block import (
7+
BlockNotFound,
8+
UnsupportedEngine,
9+
render_block,
10+
render_block_to_string,
11+
)
712

813

914
class TestDjango(TestCase):
@@ -174,6 +179,20 @@ def test_request_context(self) -> None:
174179

175180
self.assertEqual(result, "/dummy-url")
176181

182+
@modify_settings(
183+
INSTALLED_APPS={
184+
"prepend": [
185+
"django.contrib.auth",
186+
"django.contrib.contenttypes",
187+
],
188+
},
189+
)
190+
def test_render_block(self) -> None:
191+
"""Test rendering an individual block to a response."""
192+
request = RequestFactory().get("dummy-url")
193+
response = render_block(request, "test1.html", "block1")
194+
self.assertEqual(response.content, b"block1 from test1")
195+
177196

178197
@override_settings(
179198
TEMPLATES=[
@@ -185,7 +204,7 @@ def test_request_context(self) -> None:
185204
]
186205
)
187206
class TestJinja2(TestCase):
188-
"""Test the Django templating engine."""
207+
"""Test the Jinja2 templating engine."""
189208

190209
def assertExceptionMessageEquals(self, exception: Exception, expected: str) -> None:
191210
self.assertEqual(expected, exception.args[0])
@@ -271,3 +290,17 @@ def test_context(self) -> None:
271290
data = "block2 from test5"
272291
result = render_block_to_string("test5.html", "block2", {"foo": data})
273292
self.assertEqual(result, data)
293+
294+
@modify_settings(
295+
INSTALLED_APPS={
296+
"prepend": [
297+
"django.contrib.auth",
298+
"django.contrib.contenttypes",
299+
],
300+
},
301+
)
302+
def test_render_block(self) -> None:
303+
"""Test rendering an individual block to a response."""
304+
request = RequestFactory().get("dummy-url")
305+
response = render_block(request, "test1.html", "block1")
306+
self.assertEqual(response.content, b"block1 from test1")

tox.ini

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
[tox]
77
envlist =
8-
py{38,39,310,311,312}-django42,
9-
# Django 5.0 drops support for Python 3.8 and 3.9.
10-
py{310,311,312}-django{50,51,main},
11-
# Django 4.1.3 adds support for Python 3.11.
12-
py311-django{41,42,main}
8+
# See https://www.djangoproject.com/download/#supported-versions
9+
# See https://docs.djangoproject.com/en/dev/faq/install/#what-python-version-can-i-use-with-django
10+
py{39,310,311,312}-django42,
11+
py{310,311,312,313}-django{51,52},
12+
py{312,313}-djangomain
1313
isolated_build = True
1414
skip_missing_interpreters = True
1515

@@ -18,7 +18,7 @@ commands =
1818
python manage.py test
1919
deps =
2020
Jinja2
21-
django42: Django>=4.2,<4.3
22-
django50: Django>=5.0,<5.1
23-
django51: Django>=5.1b1,<5.2
21+
django42: Django>=4.2.8,<4.3
22+
django51: Django>=5.1.3,<5.2
23+
django52: Django>=5.2,<5.3
2424
djangomain: https://codeload.github.com/django/django/zip/main

0 commit comments

Comments
 (0)