Fieldsets for Views

  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

  1. Multiple-Submit-Button Widget for Choice Field by Archatas 5 years, 8 months ago
  2. FieldsetForm by Ciantic 7 years ago
  3. BetterForm with fieldsets and row_attrs by carljm 5 years, 2 months ago
  4. Form splitting/Fieldset templatetag by peritus 5 years, 7 months ago
  5. SectionedForm by marinho 5 years, 10 months ago

Comments

(Forgotten your password?)