{"id":13780493,"url":"https://github.com/clokep/django-render-block","last_synced_at":"2025-05-16T14:05:40.510Z","repository":{"id":8908054,"uuid":"59975254","full_name":"clokep/django-render-block","owner":"clokep","description":"Render the content of a specific block tag from a Django template.","archived":false,"fork":false,"pushed_at":"2025-05-09T20:40:07.000Z","size":110,"stargazers_count":254,"open_issues_count":11,"forks_count":23,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-05-09T21:26:33.634Z","etag":null,"topics":["django","django-application","django-templates","jinja2"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/clokep.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGELOG.rst","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"clokep"}},"created_at":"2016-05-30T02:36:23.000Z","updated_at":"2025-04-24T09:48:30.000Z","dependencies_parsed_at":"2025-04-12T10:32:26.572Z","dependency_job_id":"0fe3614d-53f9-48e9-bb25-56a483027e1c","html_url":"https://github.com/clokep/django-render-block","commit_stats":{"total_commits":149,"total_committers":15,"mean_commits":9.933333333333334,"dds":0.2214765100671141,"last_synced_commit":"7aed7f321243b8970382574c644c00d59bdf9a8a"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clokep%2Fdjango-render-block","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clokep%2Fdjango-render-block/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clokep%2Fdjango-render-block/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clokep%2Fdjango-render-block/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/clokep","download_url":"https://codeload.github.com/clokep/django-render-block/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253328126,"owners_count":21891428,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django","django-application","django-templates","jinja2"],"created_at":"2024-08-03T18:01:16.356Z","updated_at":"2025-05-16T14:05:40.505Z","avatar_url":"https://github.com/clokep.png","language":"Python","readme":"Django Render Block\n###################\n\n.. image:: https://img.shields.io/pypi/v/django-render-block.svg\n    :target: https://pypi.org/project/django-render-block/\n\n.. image:: https://github.com/clokep/django-render-block/actions/workflows/main.yml/badge.svg\n    :target: https://github.com/clokep/django-render-block/actions/workflows/main.yml\n\nRender the content of a specific block tag from a Django template. Works for\narbitrary template inheritance, even if a block is defined in the child template\nbut not in the parent. Generally it works like ``render_to_string`` from Django,\nbut allows you to specify a block to render.\n\nFeatures\n========\n\n*   Render a specific block from a template\n*   Fully supports the Django templating engine\n*   Partially supports the `Jinja2 \u003chttp://jinja.pocoo.org/\u003e`__ engine: it does\n    not currently process the ``extends`` tag.\n\nRequirements\n============\n\nDjango Render Block supports Django 4.2, 5.1, and 5.2 on Python 3.9, 3.10, 3.11,\n3.12, and 3.13 (see the `Django documentation \u003chttps://docs.djangoproject.com/en/dev/faq/install/#what-python-version-can-i-use-with-django\u003e`_\nfor which versions of Python are supported by particular Django versions).\n\nExamples\n========\n\nIn ``test1.html``:\n\n.. code-block:: jinja\n\n    {% block block1 %}block1 from test1{% endblock %}\n    {% block block2 %}block2 from test1{% endblock %}\n\nIn ``test2.html``:\n\n.. code-block:: jinja\n\n    {% extends 'test1.html' %}\n    {% block block1 %}block1 from test2{% endblock %}\n\nAnd from the Python shell:\n\n.. code-block:: python\n\n    \u003e\u003e\u003e from render_block import render_block_to_string\n    \u003e\u003e\u003e print(render_block_to_string('test2.html', 'block1'))\n    'block1 from test2'\n    \u003e\u003e\u003e print(render_block_to_string('test2.html', 'block2'))\n    'block2 from test1'\n\nIt can also accept a context as a ``dict`` (just like ``render_to_string``), in\n``test3.html``:\n\n.. code-block:: jinja\n\n    {% block block3 %}Render this {{ variable }}!{% endblock %}\n\nAnd from Python:\n\n.. code-block:: python\n\n    \u003e\u003e\u003e print(render_block_to_string('test3.html', 'block3', {'variable': 'test'}))\n    'Render this test!'\n\nAPI Reference\n=============\n\nThe API is simple and attempts to mirror the built-in ``render_to_string`` and ``render`` API.\n\n``render_block_to_string(template_name, block_name, context=None, request=None)``\n\n    ``template_name``\n        The name of the template to load and render. If it’s a list of template\n        names, Django uses ``select_template()`` instead of ``get_template()``\n        to find the template.\n\n    ``block_name``\n        The name of the block to render from the above template.\n\n    ``context``\n        A ``dict`` to be used as the template’s context for rendering. A ``Context``\n        object can be provided for Django templates.\n\n        ``context`` is optional. If not provided, an empty context will be used.\n\n    ``request``\n        The request object used to render the template.\n\n        ``request`` is optional and works only for Django templates. If both context and request\n        are provided, a ``RequestContext`` will be used instead of a ``Context``.\n\nSimilarly there is a ``render_block`` function which returns an `HttpResponse` with\nthe content sent to the result of ``render_block_to_string`` with the same parameters.\n\n``render_block(request, template_name, block_name, context=None, content_type=\"text/html\", status=200)``\n\n    ``request``\n        The request object used to render the template.\n\n    ``template_name``\n        The name of the template to load and render. If it’s a list of template\n        names, Django uses ``select_template()`` instead of ``get_template()``\n        to find the template.\n\n    ``block_name``\n        The name of the block to render from the above template.\n\n    ``context``\n        A ``dict`` to be used as the template’s context for rendering. A ``Context``\n        object can be provided for Django templates.\n\n        ``context`` is optional. If not provided, an empty context will be used.\n\n    ``content_type``\n        A ``str`` content type for the HTTP response.\n\n    ``status``\n        An ``int`` HTTP status code for the HTTP response.\n\nExceptions\n----------\n\nLike ``render_to_string`` this will raise the following exceptions:\n\n    ``TemplateDoesNotExists``\n        Raised if the template(s) specified by ``template_name`` cannot be\n        loaded.\n\n    ``TemplateSyntaxError``\n        Raised if the loaded template contains invalid syntax.\n\nThere are also two additional errors that can be raised:\n\n    ``BlockNotFound``\n        Raised if the block given by ``block_name`` does not exist in the\n        template.\n\n    ``UnsupportedEngine``\n        Raised if a template backend besides the Django backend is used.\n\nContributing\n============\n\nIf you find a bug or have an idea for an improvement to Django Render Block,\nplease\n`file an issue \u003chttps://github.com/clokep/django-render-block/issues/new\u003e`_ or\nprovide a pull request! Check the\n`list of issues \u003chttps://github.com/clokep/django-render-block/issues/\u003e`_ for\nideas of what to work on.\n\nAttribution\n===========\n\nThis is based on a few sources:\n\n* Originally `Django Snippet 769 \u003chttps://djangosnippets.org/snippets/769/\u003e`__\n* Updated version `Django Snippet 942 \u003chttps://djangosnippets.org/snippets/942/\u003e`__\n* A version of the snippets was ported as `Django-Block-Render \u003chttps://github.com/uniphil/Django-Block-Render/\u003e`_\n* Additionally inspired by part of `django-templated-email \u003chttps://github.com/BradWhittington/django-templated-email/blob/master/templated_email/utils.py\u003e`_\n* Also based on a `StackOverflow answer 2687173 \u003chttp://stackoverflow.com/questions/2687173/django-how-can-i-get-a-block-from-a-template\u003e`_\n","funding_links":["https://github.com/sponsors/clokep"],"categories":["Third Party Packages 📦 \u003ca name = \"tools\"\u003e\u003c/a\u003e","Python"],"sub_categories":["Helper Libraries"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclokep%2Fdjango-render-block","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fclokep%2Fdjango-render-block","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclokep%2Fdjango-render-block/lists"}