{ "info": { "author": "Benjamin Mampaey", "author_email": "bmampaey@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Environment :: Web Environment", "Framework :: Django", "Framework :: Django :: 2.0", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "# django-boundfield-renderer\nA rendering engine for Django forms using templatetags\n\n## Preamble\nThere are already many libraries to render Django forms in templates, including the default Form methods as_p(), as_ul(), as_table(). Unfortunately, none of them (that I have encountered at least) apply strictly the concept of separation of form and function, or business logic from presentation, etc. Django has made a step into the right direction by using templates for the widgets, but the entirety of a field rendering is still partially done in python code. For example, if the developer wishes to put the label after the checkbox, he still has to write python, instead of redefining a template.\n\nThis library defines one or more registry, that associate a rendering function (called renderer) for each form field class. That rendering function is made available through the templatetag `renderer`.\n\n## Installation\nAdd `'boundfield_renderer'` to your INSTALLED_APPS setting so Django can find the templatetag `renderer`.\n\n## Basic steps to render a Django Form\n### Create a registry and register the renderers for the field classes\nFor example, in the file *path/to/my_registry.py*\n```python\nfrom django import forms\nfrom django.template.loader import get_template\nfrom boundfield_renderer import RendererRegistry\n\n# Create the registry\nregistry = RendererRegistry()\n\n# Define a renderer function for a field class, for example the CharField\n# This can be as simple as the render method of a template\ntemplate_render = get_template('mytemplates/char_field.html').render\n\n# Register the renderer for the field class in the registry\n# Either by setting it directly on the registry\nregistry[forms.CharField] = template_render\n\n# Or by using the register method as a decorator on a field class definition\n@registry.register(get_template('mytemplates/my_field.html').render)\nclass MyField(forms.Field):\n\tpass\n\n```\n\nThe file *mytemplates/char_field.html* could be as simple as this:\n```html\n\n

{{ help_text }}

\n\n```\n\n### Write a template that renders the form using the `renderer` template tag\nFor example, in the file *mytemplates/my_form.html*\n```html\n{% load boundfield_renderer %}\n\n\t\n\t\t
\n\t\t\t{% for boundfield in form %}\n\t\t\t{% renderer boundfield 'path.to.my_registry.registry' %}\n\t\t\t{% endfor %}\n\t\t\t\n\t\t
\n\t\n\n```\n\n## Renderer function\nA renderer function does not have to be a template render method, it can be any method that returns a string. (Must it be marked safe?) It is passed a single parameter that is a dict (called context) with the following key/values:\n\n * __form__: The form the field belongs to\n * __boundfield__: The bound field itself\n * __html_name__: The HTML name attribute for the field\n * __value__: The current value of the field\n * __label__: The label of the field\n * __id__: The id for the label of the field\n * __help_text__: The help_text of the field\n * __errors__: The list of Validation errors for the field\n * __disabled__: True if the field is disabled\n * __is_hidden__: True if the field is hidden\n * __required__: True if the field is required\n\n\n## Resolution of renderer\nTo find a renderer for a field class, we follow the MRO order of the field class until we find a renderer registered for that subclass. For example:\n\n```python\nfrom django.core.exceptions import ValidationError\n\n# Override CharField to add some extra clean, BUT DO NOT register a new renderer\nclass MyCharField(forms.CharField):\n\tdef clean(self, value):\n\t\tif value == 'Spanish Inquisition':\n\t\t\traise ValidationError('I was not expecting the Spanish Inquisition!')\n\t\telse:\n\t\t\treturn super().clean(value)\n\n# Override MyCharField AND register a new renderer\n@registry.register(get_template('mytemplates/my_other_char_field.html').render)\nclass MyOtherCharField(MyCharField):\n\tpass\n\n```\n\nIn the first case, the renderer used will be the one we registered for the CharField earlier (template *mytemplates/char_field.html*). In the second, the renderer used will be the one we registered for MyOtherCharField (template *mytemplates/my_other_char_field.html*)\n\nIf no renderer is found, a ValueError is raised.\n\n## *renderer* templatetag\nThe renderer templatetag takes a boundfield as the first parameter. It can also take as a second optional parameter a registry object or the full dotted path to a registry object.\n\nIf no registry is specified, it will use the default registry as defined in the settings as DEFAULT_BOUNDFIELD_RENDERER_REGISTRY.\n\n\nFor our example this would be:\n```python\nDEFAULT_BOUNDFIELD_RENDERER_REGISTRY = 'path.to.my_registry.registry'\n```\n\nFinally, the renderer tag can take optional keyword parameters that will be added to the context.\n\nIf we reuse our previous example form template *mytemplates/my_form.html*, we could change the context for a particular field \"starwberry\" to add a context variable \"css_classes\" with a value of \"forever\":\n\n```html\n{% load boundfield_renderer %}\n\n\t\n\t\t
\n\t\t\t{% for boundfield in form %}\n\t\t\t{% if boundfield.name == 'strawberry' %}\n\t\t\t{% renderer boundfield 'path.to.my_registry.registry' css_classes='forever' %}\n\t\t\t{% else %}\n\t\t\t{% renderer boundfield 'path.to.my_registry.registry' %}\n\t\t\t{% endif %}\n\t\t\t{% endfor %}\n\t\t\t\n\t\t
\n\t\n\n```\n\n## Note on renderer registration\nThe code that registers the renderer for a form field class must be executed by Django before the `renderer` templatetag is called.\n\nPractically, this can be achieved by registering the renderers in the same file where the registry is defined, or in the files where the form field class are defined (for example using the *register* decorator)\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "https://github.com/bmampaey/django-boundfield-renderer/tarball/master", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/bmampaey/django-boundfield-renderer", "keywords": "django,form", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "django-boundfield-renderer", "package_url": "https://pypi.org/project/django-boundfield-renderer/", "platform": "any", "project_url": "https://pypi.org/project/django-boundfield-renderer/", "project_urls": { "Download": "https://github.com/bmampaey/django-boundfield-renderer/tarball/master", "Homepage": "https://github.com/bmampaey/django-boundfield-renderer" }, "release_url": "https://pypi.org/project/django-boundfield-renderer/1.4/", "requires_dist": [ "Django (>=2.0)" ], "requires_python": ">= 3.5", "summary": "A rendering engine for Django forms using templatetags", "version": "1.4" }, "last_serial": 5440921, "releases": { "1.0": [ { "comment_text": "", "digests": { "md5": "0421c651829ae458217d67566ecb5218", "sha256": "6cdfa25ca3fdab4fe10a564f80159a396b895a8d55861d774994691d29c69644" }, "downloads": -1, "filename": "django_boundfield_renderer-1.0-py3-none-any.whl", "has_sig": false, "md5_digest": "0421c651829ae458217d67566ecb5218", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 10509, "upload_time": "2019-04-09T14:21:21", "url": "https://files.pythonhosted.org/packages/31/74/f5559e5680985b6173f2072f72f8e3279443c06690924f42672c9fbbf7cf/django_boundfield_renderer-1.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "59fb27ef3ad5bdb33170fb3a6d9c4b54", "sha256": "e13d2672f7d8a8e97c8a156dfb510c1267c3ce333007f2f5faf5a0688e9deabe" }, "downloads": -1, "filename": "django-boundfield-renderer-1.0.tar.gz", "has_sig": false, "md5_digest": "59fb27ef3ad5bdb33170fb3a6d9c4b54", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12851, "upload_time": "2019-04-09T14:21:23", "url": "https://files.pythonhosted.org/packages/29/f8/ded83b256cd1d17b891109ffa63ee12abd3fbc36a6b3fffc0656b37440f2/django-boundfield-renderer-1.0.tar.gz" } ], "1.1": [ { "comment_text": "", "digests": { "md5": "216c6992c386a6b44e76f7454e7883f2", "sha256": "c01077c968a5b613dd49c5e64ee12a27e46d1f27a79da3cf0801f73d06c0b8cb" }, "downloads": -1, "filename": "django_boundfield_renderer-1.1-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "216c6992c386a6b44e76f7454e7883f2", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">= 3.5", "size": 10540, "upload_time": "2019-04-09T20:36:14", "url": "https://files.pythonhosted.org/packages/23/af/ac181c64be2f222a8d419b6e5ffc85c796a5ec8e7c582d1716ca2c3aa9a3/django_boundfield_renderer-1.1-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "c8ef6c02388dc8b10e3ad21a6a208ef1", "sha256": "fba2179966b1571a40d9660a8442b80b8286ec8bcee4016112b3ea8cbf15b0ea" }, "downloads": -1, "filename": "django-boundfield-renderer-1.1.tar.gz", "has_sig": false, "md5_digest": "c8ef6c02388dc8b10e3ad21a6a208ef1", "packagetype": "sdist", "python_version": "source", "requires_python": ">= 3.5", "size": 6867, "upload_time": "2019-04-09T20:36:15", "url": "https://files.pythonhosted.org/packages/06/28/71e99806e2d4cce1995734731110cf42f6481240b4e585cb4421b240483b/django-boundfield-renderer-1.1.tar.gz" } ], "1.2": [ { "comment_text": "", "digests": { "md5": "f2c6aabc292c45137048bb7c4b1044f3", "sha256": "4552d41cb2e230cd35ff4bd7fa3c3d2fb84e0dcc9fffaf9136017e10c33cacd2" }, "downloads": -1, "filename": "django_boundfield_renderer-1.2-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "f2c6aabc292c45137048bb7c4b1044f3", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">= 3.5", "size": 10662, "upload_time": "2019-04-10T08:09:23", "url": "https://files.pythonhosted.org/packages/9a/87/9631fb4fe790ad1aabd3e8418d18fce82100dd91285187089c88acd45727/django_boundfield_renderer-1.2-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "e5abfba11d07db87951205389906a6f6", "sha256": "7f7d73c26fd4de54dd8db2bc889c9818d6c8b0d4b2f484d62993b9a1218d1e25" }, "downloads": -1, "filename": "django-boundfield-renderer-1.2.tar.gz", "has_sig": false, "md5_digest": "e5abfba11d07db87951205389906a6f6", "packagetype": "sdist", "python_version": "source", "requires_python": ">= 3.5", "size": 6995, "upload_time": "2019-04-10T08:09:24", "url": "https://files.pythonhosted.org/packages/91/08/728ba8eda8a31a5c2f9f7861f191961194ad2c1eb59126c8ce354aef4c4c/django-boundfield-renderer-1.2.tar.gz" } ], "1.3": [ { "comment_text": "", "digests": { "md5": "e6ebaf99113c07ae3dbbb3026b5d7535", "sha256": "d7a91ae8952a7256ece4e1fc28b4aeedb8b82c34000bf1b17c06e788227e6eeb" }, "downloads": -1, "filename": "django_boundfield_renderer-1.3-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "e6ebaf99113c07ae3dbbb3026b5d7535", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">= 3.5", "size": 10778, "upload_time": "2019-06-20T11:31:07", "url": "https://files.pythonhosted.org/packages/21/35/a338e206149c2e701129e9d05987069a4f314391e6ac5b6315415f86c622/django_boundfield_renderer-1.3-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "cb2e0f32d0c31d2858818dd98df95190", "sha256": "5c8cb8a594a983214735907df5db845941100385284c736fd36b563633145282" }, "downloads": -1, "filename": "django-boundfield-renderer-1.3.tar.gz", "has_sig": false, "md5_digest": "cb2e0f32d0c31d2858818dd98df95190", "packagetype": "sdist", "python_version": "source", "requires_python": ">= 3.5", "size": 7122, "upload_time": "2019-06-20T11:31:09", "url": "https://files.pythonhosted.org/packages/4b/d2/0a6b065b2910d9b4bc0f0c91928e5ece3658d0020b3ce1e4c408d7d3ff42/django-boundfield-renderer-1.3.tar.gz" } ], "1.4": [ { "comment_text": "", "digests": { "md5": "5f9bc93506e52773b7d7a04d85c8c3f8", "sha256": "698df3a55b58b9d4c56c6703c32246defcb021ccbcd101cd5cf618464ecd4889" }, "downloads": -1, "filename": "django_boundfield_renderer-1.4-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "5f9bc93506e52773b7d7a04d85c8c3f8", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">= 3.5", "size": 10776, "upload_time": "2019-06-24T14:40:15", "url": "https://files.pythonhosted.org/packages/a0/03/c44b7d6788040f49839a4f0138115f066e28286dc8359ab2858b71462de2/django_boundfield_renderer-1.4-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "3e7840602d3307ae8908587a320d7868", "sha256": "01192f0393594af6d15d80f77c509b1f392b1c3fedbe5addd18b39463524c1df" }, "downloads": -1, "filename": "django-boundfield-renderer-1.4.tar.gz", "has_sig": false, "md5_digest": "3e7840602d3307ae8908587a320d7868", "packagetype": "sdist", "python_version": "source", "requires_python": ">= 3.5", "size": 7154, "upload_time": "2019-06-24T14:40:17", "url": "https://files.pythonhosted.org/packages/2e/ae/7598be61b3ac790130ad5ba85cae40a343b96317a74149d8ce01b840d020/django-boundfield-renderer-1.4.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "5f9bc93506e52773b7d7a04d85c8c3f8", "sha256": "698df3a55b58b9d4c56c6703c32246defcb021ccbcd101cd5cf618464ecd4889" }, "downloads": -1, "filename": "django_boundfield_renderer-1.4-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "5f9bc93506e52773b7d7a04d85c8c3f8", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">= 3.5", "size": 10776, "upload_time": "2019-06-24T14:40:15", "url": "https://files.pythonhosted.org/packages/a0/03/c44b7d6788040f49839a4f0138115f066e28286dc8359ab2858b71462de2/django_boundfield_renderer-1.4-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "3e7840602d3307ae8908587a320d7868", "sha256": "01192f0393594af6d15d80f77c509b1f392b1c3fedbe5addd18b39463524c1df" }, "downloads": -1, "filename": "django-boundfield-renderer-1.4.tar.gz", "has_sig": false, "md5_digest": "3e7840602d3307ae8908587a320d7868", "packagetype": "sdist", "python_version": "source", "requires_python": ">= 3.5", "size": 7154, "upload_time": "2019-06-24T14:40:17", "url": "https://files.pythonhosted.org/packages/2e/ae/7598be61b3ac790130ad5ba85cae40a343b96317a74149d8ce01b840d020/django-boundfield-renderer-1.4.tar.gz" } ] }