This Snippet allows a view to controle the printed forms on the templates, in a similar way to the fieldsets used by the django admin.
How to Use:
In the view in question, put:
def some_view(request):
...
fieldsets = (
(u'Title 1',
{'hidden' : ('field_1', 'field_2',),
'fields' : ('field_3',)}),
(u'Title 2',
{'hidden' : ('field_5', 'field_6',),
'fields' : ('field_4',)}),)
)
return render_to_response('some.html', {'fieldsets': fieldsets})
fieldsets = (
(None,
{'hidden' : ('evento', 'colaborador',),
'fields' : ('acompanhantes',)}),
)
Next, in the html just add:
<form enctype="multipart/form-data" id="edit" method="post" ...>
...
{% include "inc/form_snippet.html" %}
...
<input type="submit" value="Submit">
</form>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | <!-- form_snippet.html -->
{% load forms %}
{{form.media}}
{% for title,dict in fieldsets %}
<fieldset class="module aligned">
{% if title %}<div class="title">{{title}}</div>{% endif %}
{% for field in dict.hidden %}
<div class="form-row hidden">
{% form_field form field context 'single_field' %}
<div>
<div class="form_item">
<div>{{ single_field }}</div>
</div>
<div class="clear"></div>
</div>
</div>
{% endfor %}
{% for field in dict.fields %}
<div class="form-row">
{% form_field form field context 'single_field' %}
{% if single_field.errors %}
<div class="form_errors">{{ single_field.errors }}</div>
{% endif %}
<div>
<div class="form_label {% if single_field.errors %}form_label_required{% endif %}">{{ single_field.label }}:</div>
<div class="form_item">
{% if single_field|is_filefield_instance and single_field|field_value %}
<div>Actualmente: <a target="_blank" href="{{ single_field|url }}">{{ single_field|field_value }}</a></div>
{% endif %}
<div>{{ single_field }}</div>
{% if single_field.help_text %}
<div class="form_help_text clear">{{ single_field.help_text }}</div>
{% endif %}
</div>
<div class="clear"></div>
</div>
</div>
{% endfor %}
</fieldset>
{% endfor %}
### put in forms.py under templatetags ###
from django.template import Library
from django.forms import ChoiceField
from django.forms.fields import FileField
register = Library()
@register.simple_tag
def form_field(form, field, context, tag):
"""
Usage: form_field form field context 'tag'
"""
context[tag] = form.__getitem__(field)
return ''
@register.filter(name='field_value')
def field_value(field):
"""
Returns the value of a form field
"""
try:
value = field.form.initial[field.name]
if not value:
return ''
if isinstance(field.field, ChoiceField):
for (val, desc) in field.field.choices:
if val == value:
return desc
return value
except:
return ''
@register.filter(name='is_filefield_instance')
def is_filefield_instance(field):
return isinstance(field.field, FileField)
/***** form_snippet.css ********/
.module { margin-bottom: 5px; background: #ffffff; }
.aligned .form_label { display: block; float: left; width: 25%; margin-right: 2%; }
.aligned .form_item { display: block; float: left; width: 73%; text-align: left !important; }
.aligned .form_item input[type="checkbox"],
.aligned .form_item input[type="radio"] { width: auto; float: left; text-align: left; margin-right: 5px; }
.aligned .form_item li { width: 33%; float: left; }
.form-row { font-size: 11px; padding-bottom: 10px; }
.form_label { font-weight: normal !important; color: #666; font-size: 12px; }
.form_item input, textarea, select { background: #f8f6f6; border: 1px solid #ccc; color: #666666; width: 98%; }
.form_errors { background: #e20019; color: #ffffff; padding: 1px 5px 1px 5px; margin-bottom: 5px; }
.form_label_required { color: #e20019; }
.form_help_text { font-size: 8px; color: #999999; }
.form_submit { text-align: right; }
.hidden { display: none; }
.clear { clear: both; }
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.