Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added some form display helpers (template tags and HTML fragments).

  • Loading branch information...
commit 9b2e85db107a54021eace203a78eab0d62527c5a 1 parent e4f6bad
@malcolmt authored
View
2  general/README
@@ -0,0 +1,2 @@
+Useful little things for Django that don't necessarily belong in any particular app on their own.
+
View
0  general/__init__.py
No changes.
View
14 general/templates/general/form_tag.html
@@ -0,0 +1,14 @@
+{% if form.non_field_errors %}<tr>
+ <td colspan="3" class="errors">{{ form.non_field_errors }}</td>
+</tr>{% endif %}
+{% for field in form.visible_fields %}<tr>
+ <th>{% if show_required and field.field.required and field.field.widget.input_type %}
+ <span class="required">(*)</span>{% endif %}
+ {{ field.label }}</th>
+ <td>{{ field }}{% if forloop.first %}
+ <input type='hidden' name='csrfmiddlewaretoken' value='{{ csrf_token }}' />
+ {% for hidden in form.hidden_fields %}{{ hidden }}{% endfor %}
+ {% endif %}</td>
+ <td class="errors">{{ field.errors }}</td>
+</tr>{% endfor %}
+
View
0  general/templatetags/__init__.py
No changes.
View
17 general/templatetags/form_utils.py
@@ -0,0 +1,17 @@
+"""
+Template tags for working with forms.
+"""
+
+from django.template import Library
+
+register = Library()
+
+@register.inclusion_tag("general/form_tag.html")
+def form_layout(form, show_required=True):
+ """
+ Display a form using a standard layout fragment. If the "show-required"
+ parameter is non-false, required fields will have an asterisk before their
+ label.
+ """
+ return {"form": form, "show_required": show_required}
+
View
1  settings.py
@@ -92,6 +92,7 @@
'minerva',
'user_management',
'debug_toolbar',
+ 'general',
)
LOGIN_REDIRECT_URL = "/"
View
40 static/css/style.layout.css
@@ -49,3 +49,43 @@
background: #BFF08E;
text-align: center;
}
+
+/* Form styling */
+
+form th, form td {
+ border-color: #b7ca79;
+ line-height: 2.5em;
+}
+
+form th {
+ padding-left: 1em;
+ padding-right: 1em;
+ background-color: #b7ca79;
+ color: #333;
+ text-align: right;
+}
+
+.button_row td {
+ border-width: 0;
+ text-align: right;
+}
+
+.required {
+ color: #f00;
+ font-weight: bold;
+}
+
+form table .errors {
+ border: none;
+ color: #f00;
+ font-weight: bold;
+}
+
+.errors ul {
+ margin: 0;
+}
+
+.errors li {
+ list-style: none;
+}
+
View
9 user_management/templates/user_management/create.html
@@ -1,11 +1,12 @@
{% extends "base.html" %}
+{% load form_utils %}
{% block content %}
<form action="" method="post">{% csrf_token %}
- <table>
- {{ user_create_form }}
- {{ user_profile_form }}
- <tr><td></td><td><input type="submit" value="Create User" /></td></tr>
+ <table>{% form_layout user_create_form %}{% form_layout user_profile_form %}
+ <tr class="button_row"><td></td>
+ <td><input type="submit" value="Create User" /></td>
+ </tr>
</table>
</form>
{% endblock %}
View
13 user_management/templates/user_management/login_form.html
@@ -1,12 +1,13 @@
+{% load form_utils %}
<form id="login-form" {% if hidden %}class="hidden"{% endif %} action="{% url login %}" method="post">
<p><a href="{% url signup %}">Sign up as a new user</a></p>
- {% csrf_token %}
- <table>
- {{ form }}
+ <table>{% form_layout form %}
<tr><td></td>
- <td><input type="submit" value="Login" />
- <a id="login-cancel" href="{% url question-page %}">Cancel</a>
- </td></tr>
+ <td><input type="submit" value="Login" /></td>
+ <td><a id="login-cancel" href="{% url question-page %}">Cancel</a>
+ </td>
+ </tr>
</table>
</form>
+{# vim: se syn=htmldjango: #}
Please sign in to comment.
Something went wrong with that request. Please try again.