Package: django-render-block
Django Render BlockRender the content of a specific block tag from a Django template. Works for
arbitrary template inheritance, even if a block is defined in the child template
but not in the parent. Generally it works likerender_to_stringfrom Django,
but allows you to specify a block to render.FeaturesRender a specific block from a templateFully supports the Django templating enginePartially supports theJinja2engine: it does
not currently process theextendstag.RequirementsDjango Render Block supports Django 2.2, 3.1, 3.2, 4.0, and 4.1 on Python 3.7, 3.8,
3.9, and 3.10 (see the Django documentation for which versions of Python are
supported by particular Django versions).ExamplesIntest1.html:{%blockblock1%}block1 from test1{%endblock%}{%blockblock2%}block2 from test1{%endblock%}Intest2.html:{%extends'test1.html'%}{%blockblock1%}block1 from test2{%endblock%}And from the Python shell:>>>fromrender_blockimportrender_block_to_string>>>printrender_block_to_string('test2.html','block1')u'block1 from test2'>>>printrender_block_to_string('test2.html','block2')u'block2 from test1'It can also accept a context as adict(just likerender_to_string), intest3.html:{%blockblock3%}Render this{{variable}}!{%endblock%}And from Python:>>>printrender_block_to_string('test3.html','block3',{'variable':'test'})u'Render this test!'API ReferenceThe API is simple and attempts to mirror the built-inrender_to_stringAPI.render_block_to_string(template_name, block_name, context=None, request=None)template_nameThe name of the template to load and render. If it’s a list of template
names, Django usesselect_template()instead ofget_template()to find the template.block_nameThe name of the block to render from the above template.contextAdictto be used as the template’s context for rendering. AContextobject can be provided for Django templates.contextis optional. If not provided, an empty context will be used.requestThe request object used to render the template.requestis optional and works only for Django templates. If both context and request
are provided, aRequestContextwill be used instead of aContext.ExceptionsLikerender_to_stringthis will raise the following exceptions:TemplateDoesNotExistsRaised if the template(s) specified bytemplate_namecannot be
loaded.TemplateSyntaxErrorRaised if the loaded template contains invalid syntax.There are also two additional errors that can be raised:BlockNotFoundRaised if the block given byblock_namedoes not exist in the
template.UnsupportedEngineRaised if a template backend besides the Django backend is used.ContributingIf you find a bug or have an idea for an improvement to Django Render Block,
pleasefile an issueor
provide a pull request! Check thelist of issuesfor
ideas of what to work on.AttributionThis is based on a few sources:OriginallyDjango Snippet 769Updated versionDjango Snippet 942A version of the snippets was ported asDjango-Block-RenderAdditionally inspired by part ofdjango-templated-emailAlso based on aStackOverflow answer 2687173Changelog0.9.2 (October 18, 2022)MaintenanceDrop support for Python 3.6. (#36)Improve package metadata. (#37)Runblack,isort,
andflake8, andpyupgrade.
(#38,#39)Update to include and run tests for Django 4.1. Contributed byJack Linke.
(#41)0.9.1 (December 15, 2021)MaintenanceSupport Python 3.10. (#33)Fixed a packaging issue where the generated wheels were empty. Contributed
by@cordery. (#35)0.9 (December 14, 2021)MaintenanceDrop support for Django 3.0. (#31)Support Django 3.2 and 4.0. (#27,#31)Switch continuous integration to GitHub Actions. (#26,#28)Changed packaging to use setuptools declarative config insetup.cfg.
(#32)0.8.1 (October 15, 2020)BugfixesFixes a regression in v0.8 where aContextcould not be re-used. Contributed
by@evanbrumley. (#25)0.8 (October 6, 2020)Bugfixesrender_block_to_stringnow forwards theContextpassed ascontextparameter.
Contributed by@bblanchon. (#21)MaintenanceDrop support for Python 3.5, support Python 3.9. (#22)0.7 (July 13, 2020)MaintenanceDrop support for Django < 2.2. (#18)Support Django 3.0 and 3.1. (#18,#20)Drop support for Python 2.7. (#19)Support Python 3.8. (#18)0.6 (May 8, 2019)Improvementsrender_block_to_stringnow optionally accepts arequestparameter.
If given, aRequestContextinstead of aContextis used when
rendering with the Django templating engine. Contributed by@vintage. (#15)MaintenanceSupport Django 1.11, 2.1, and 2.2. (#9,#11,#17)Support Python 2.7, 3.5, 3.6, and 3.7. (#9,#17)Fix rendering of README on PyPI. Contributed by@mixxorz.
(#10)0.5 (September 1, 2016)BugfixesFixes a major issue with inheriting templates and rendering a block found in
the parent template, but overwriting part of it in the child template.
(#8)0.4 (August 4, 2016)ImprovementsInitial support for using theJinja2templating
engine. See README for caveats. (#3)MaintenanceSupport Django 1.10. (#5)Support Python 3. (#6)0.3.1 (June 1, 2016)MaintenanceRefactoring to make more generic (for potentially supporting multiple
templating engines).0.3 (May 27, 2016)Largely rewritten.Support Django 1.8 and 1.9:Guards against different template backends.Uses internal APIs for each node.Removedcontext_instanceparameter.Support for calling{{ block.super }}.0.2.2 (January 10, 2011)Updated percomment 3466 on Django Snippet 942byeugenybogerto fix an issue with nested extends. The specific bug was not reproducible,
but the additional code shouldn’t hurt.0.2.1 (August 27, 2010)Updated percomment 3237 on Django Snippet 942bychadselphto remove a pointless render. The specific bug was not reproducible, but the
removed code was extraneous.0.2 (August 4, 2008)Updated version fromDjango Snippet 942byzbyte64.Improves include:Simpler/better handling of “extends” block tagSearches If/Else blocksLess codeAllow list of templates to be passed which is closer to the behavior ofrender_to_response0.1 (May 22, 2008)Initial version fromDjango Snippet 769bysciyoshi.Support Django 0.96.