Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/prestontimmons/djformtags
Better form rendering for Django
https://github.com/prestontimmons/djformtags
Last synced: 2 months ago
JSON representation
Better form rendering for Django
- Host: GitHub
- URL: https://github.com/prestontimmons/djformtags
- Owner: prestontimmons
- Created: 2011-05-19T22:00:29.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2014-10-14T00:01:34.000Z (over 10 years ago)
- Last Synced: 2024-10-30T15:55:48.326Z (3 months ago)
- Language: Python
- Homepage:
- Size: 211 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- starred-awesome - djformtags - Better form rendering for Django (Python)
README
# Control Django form field rendering from within the template
This app provides template tags that simplify and enhance form rendering
in Django templates.## One: Set custom attributes on a form field
Say you have the following form with an email field.
```python
class EmailForm(forms.Form):
email = forms.CharField(max_length=256)
```Now, you want to reuse this form, but optimized for a mobile device. Let's
do it in the form class:```python
class EmailField(forms.TextInput):
input_type = "email"class MobileEmailForm(EmailForm):
email = forms.CharField(
widget=forms.TextInput(attrs={
"autocapitalize": "off",
"class": "mobile-input",
"placeholder": "[email protected]",
),
)
```Really? That's ugly, it requires a new form definition, and it's messing
with stuff that belongs in the template.Enter {% setattr %}:
```html
{% load formtags %}{% setattr form.email "type" "email" %}
{% setattr form.email "autocapitalize" "off" %}
{% setattr form.email "class" "mobile-input" %}
{% setattr form.email "placeholder" "[email protected]" %}
{{ form.email }}
```And you get:
```
```
Set whatever attribute you want directly on the field right in the template
where you're rendering the form. No boilerplate needed.### Special attributes
In addition to setting an attribute, the following settings will modify
the field or widget:* type
* label
* initial
* classesNote: Using the `classes` argument will set the class to the field in
addition to the existing field class. Using `class` will override the
field class.## Two: Rendering form rows
This tag is more convenience than anything.
Start with a basic form row template, ``forms/row.html``:
```html
{{ field.label|safe }}
{% if label_help_text %}{{ label_help_text|safe }}{% endif %}
{% if field.errors %}
{% endif %}{{ field }}
```Enter {% formrow %}:
```html
{% formrow form.email %}
```You can also pass in a couple common arguments. Any extra values are passed
directly to the form row template context.```html
{% formrow form.email class="input-email" label="Email Address" template="formrow.html" foo="bar" %}
```The template will also be rendered with an ``input_type`` variable in the
context. This can be used to customize rendering for different field types.```
{% if input_type == "checkbox" %}
.. handle checkbox
{% elif input_type == "radio" %}
.. handle radio
{% elif input_type == "text" %}
.. handle text
{% endif %}
```Input type is one of the following:
```
checkbox
file
hidden
radio
select
text
textarea
```## Installation
Install with pip:
```
pip install djformtags
```Or install from github
```
pip install -e [email protected]:prestontimmons/djformtags.git#egg=djformtags
```Add ``djformtags`` to ``INSTALLED_APPS``.