author | Todd Larsen <tlarsen@google.com> |
Fri, 18 Jul 2008 18:22:23 +0000 | |
changeset 54 | 03e267d67478 |
parent 38 | soc/views/helpers/templatetags/forms_helpers.py@9a6ee3ab1446 |
child 99 | 8c38b546a3cf |
permissions | -rw-r--r-- |
38
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/python2.5 |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
2 |
# |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
3 |
# Copyright 2008 the Melange authors. |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
4 |
# |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
6 |
# you may not use this file except in compliance with the License. |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
7 |
# You may obtain a copy of the License at |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
8 |
# |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
10 |
# |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
11 |
# Unless required by applicable law or agreed to in writing, software |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
14 |
# See the License for the specific language governing permissions and |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
15 |
# limitations under the License. |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
16 |
|
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
17 |
"""A Django template tag library containing forms helpers. |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
18 |
""" |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
19 |
|
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
20 |
__authors__ = [ |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
21 |
'"Todd Larsen" <tlarsen@google.com>', |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
22 |
] |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
23 |
|
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
24 |
|
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
25 |
from django import template |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
26 |
register = template.Library() |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
27 |
|
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
28 |
|
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
29 |
@register.inclusion_tag('soc/templatetags/_field_as_table_row.html') |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
30 |
def field_as_table_row(field): |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
31 |
"""Prints a newforms field as a table row. |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
32 |
|
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
33 |
This function actually does very little, simply passing the supplied |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
34 |
form field instance in a simple context used by the _field_as_table_row.html |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
35 |
template (which is actually doing all of the work). |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
36 |
|
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
37 |
See soc/templates/soc/templatetags/_field_as_table_row.html for the CSS |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
38 |
styles used by this template tag. |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
39 |
|
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
40 |
Usage: |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
41 |
{% load forms_helpers %} |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
42 |
... |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
43 |
<table> |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
44 |
{% field_as_table_row form.fieldname %} |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
45 |
... |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
46 |
</table> |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
47 |
|
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
48 |
Args: |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
49 |
field: a Django newforms field instance |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
50 |
|
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
51 |
Returns: |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
52 |
a simple context containing the supplied newforms field instance: |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
53 |
{ 'field': field } |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
54 |
""" |
9a6ee3ab1446
A {% field_as_table_row %} template tag to simplify custom forms.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
55 |
return {'field': field} |