From cc1c4232554153ac6a24b6b4f89ecaac80b28bfd Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 15 Oct 2014 11:12:34 +0100 Subject: [PATCH] Reorganize to use template_pack in form rendering --- rest_framework/renderers.py | 15 +++------ .../fields/inline/textarea.html | 6 ---- .../templates/rest_framework/form.html | 33 ------------------- .../fields}/checkbox.html | 0 .../fields}/fieldset.html | 0 .../fields}/input.html | 0 .../fields}/list_fieldset.html | 0 .../fields}/select.html | 0 .../fields}/select_checkbox.html | 0 .../fields}/select_multiple.html | 0 .../fields}/select_radio.html | 0 .../fields}/textarea.html | 0 .../rest_framework/horizontal/form.html | 15 +++++++++ .../inline => inline/fields}/checkbox.html | 0 .../inline => inline/fields}/fieldset.html | 0 .../inline => inline/fields}/input.html | 0 .../inline => inline/fields}/select.html | 0 .../fields}/select_checkbox.html | 0 .../fields}/select_multiple.html | 0 .../fields}/select_radio.html | 0 .../inline/fields/textarea.html | 6 ++++ .../templates/rest_framework/inline/form.html | 11 +++++++ .../fields}/checkbox.html | 0 .../fields}/fieldset.html | 0 .../vertical => vertical/fields}/input.html | 0 .../fields}/list_fieldset.html | 0 .../vertical => vertical/fields}/select.html | 0 .../fields}/select_checkbox.html | 0 .../fields}/select_multiple.html | 0 .../fields}/select_radio.html | 0 .../fields}/textarea.html | 0 .../rest_framework/vertical/form.html | 11 +++++++ rest_framework/templatetags/rest_framework.py | 4 +-- 33 files changed, 49 insertions(+), 52 deletions(-) delete mode 100644 rest_framework/templates/rest_framework/fields/inline/textarea.html delete mode 100644 rest_framework/templates/rest_framework/form.html rename rest_framework/templates/rest_framework/{fields/horizontal => horizontal/fields}/checkbox.html (100%) rename rest_framework/templates/rest_framework/{fields/horizontal => horizontal/fields}/fieldset.html (100%) rename rest_framework/templates/rest_framework/{fields/horizontal => horizontal/fields}/input.html (100%) rename rest_framework/templates/rest_framework/{fields/horizontal => horizontal/fields}/list_fieldset.html (100%) rename rest_framework/templates/rest_framework/{fields/horizontal => horizontal/fields}/select.html (100%) rename rest_framework/templates/rest_framework/{fields/horizontal => horizontal/fields}/select_checkbox.html (100%) rename rest_framework/templates/rest_framework/{fields/horizontal => horizontal/fields}/select_multiple.html (100%) rename rest_framework/templates/rest_framework/{fields/horizontal => horizontal/fields}/select_radio.html (100%) rename rest_framework/templates/rest_framework/{fields/horizontal => horizontal/fields}/textarea.html (100%) create mode 100644 rest_framework/templates/rest_framework/horizontal/form.html rename rest_framework/templates/rest_framework/{fields/inline => inline/fields}/checkbox.html (100%) rename rest_framework/templates/rest_framework/{fields/inline => inline/fields}/fieldset.html (100%) rename rest_framework/templates/rest_framework/{fields/inline => inline/fields}/input.html (100%) rename rest_framework/templates/rest_framework/{fields/inline => inline/fields}/select.html (100%) rename rest_framework/templates/rest_framework/{fields/inline => inline/fields}/select_checkbox.html (100%) rename rest_framework/templates/rest_framework/{fields/inline => inline/fields}/select_multiple.html (100%) rename rest_framework/templates/rest_framework/{fields/inline => inline/fields}/select_radio.html (100%) create mode 100644 rest_framework/templates/rest_framework/inline/fields/textarea.html create mode 100644 rest_framework/templates/rest_framework/inline/form.html rename rest_framework/templates/rest_framework/{fields/vertical => vertical/fields}/checkbox.html (100%) rename rest_framework/templates/rest_framework/{fields/vertical => vertical/fields}/fieldset.html (100%) rename rest_framework/templates/rest_framework/{fields/vertical => vertical/fields}/input.html (100%) rename rest_framework/templates/rest_framework/{fields/vertical => vertical/fields}/list_fieldset.html (100%) rename rest_framework/templates/rest_framework/{fields/vertical => vertical/fields}/select.html (100%) rename rest_framework/templates/rest_framework/{fields/vertical => vertical/fields}/select_checkbox.html (100%) rename rest_framework/templates/rest_framework/{fields/vertical => vertical/fields}/select_multiple.html (100%) rename rest_framework/templates/rest_framework/{fields/vertical => vertical/fields}/select_radio.html (100%) rename rest_framework/templates/rest_framework/{fields/vertical => vertical/fields}/textarea.html (100%) create mode 100644 rest_framework/templates/rest_framework/vertical/form.html diff --git a/rest_framework/renderers.py b/rest_framework/renderers.py index 4fb360609..5fae75f2e 100644 --- a/rest_framework/renderers.py +++ b/rest_framework/renderers.py @@ -339,7 +339,6 @@ class HTMLFormRenderer(BaseRenderer): """ media_type = 'text/html' format = 'form' - template = 'rest_framework/form.html' charset = 'utf-8' field_templates = ClassLookupDict({ @@ -383,26 +382,21 @@ class HTMLFormRenderer(BaseRenderer): serializers.TimeField: 'time', }) - def render_field(self, field, layout=None): - layout = layout or 'vertical' + def render_field(self, field, template_pack=None): style_type = field.style.get('type', 'default') - if style_type == 'textarea' and layout == 'inline': - style_type = 'default' input_type = self.input_type[field] if input_type == 'datetime-local' and isinstance(field.value, six.text_type): field.value = field.value.rstrip('Z') base = self.field_templates[field][style_type] - template_name = 'rest_framework/fields/' + layout + '/' + base + template_name = template_pack + '/fields/' + base template = loader.get_template(template_name) context = Context({ 'field': field, 'input_type': input_type, 'renderer': self, - 'layout': layout }) - return template.render(context) def render(self, data, accepted_media_type=None, renderer_context=None): @@ -411,11 +405,10 @@ class HTMLFormRenderer(BaseRenderer): """ renderer_context = renderer_context or {} request = renderer_context['request'] - - template = loader.get_template(self.template) + template = loader.get_template('rest_framework/horizontal/form.html') context = RequestContext(request, { 'form': data.serializer, - 'layout': getattr(getattr(data, 'Meta', None), 'layout', 'horizontal'), + 'template_pack': 'rest_framework/horizontal', 'renderer': self }) return template.render(context) diff --git a/rest_framework/templates/rest_framework/fields/inline/textarea.html b/rest_framework/templates/rest_framework/fields/inline/textarea.html deleted file mode 100644 index a644ea382..000000000 --- a/rest_framework/templates/rest_framework/fields/inline/textarea.html +++ /dev/null @@ -1,6 +0,0 @@ -
- {% if field.label %} - - {% endif %} - -
diff --git a/rest_framework/templates/rest_framework/form.html b/rest_framework/templates/rest_framework/form.html deleted file mode 100644 index 162c56336..000000000 --- a/rest_framework/templates/rest_framework/form.html +++ /dev/null @@ -1,33 +0,0 @@ - - -{% load rest_framework %} -
- {% csrf_token %} - {% for field in form %} - {% if not field.read_only %} - {% render_field field layout=layout renderer=renderer %} - {% endif %} - {% endfor %} - - {% if layout == "horizontal" %} -
-
- -
-
- {% else %} - - {% endif %} -
- diff --git a/rest_framework/templates/rest_framework/fields/horizontal/checkbox.html b/rest_framework/templates/rest_framework/horizontal/fields/checkbox.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/horizontal/checkbox.html rename to rest_framework/templates/rest_framework/horizontal/fields/checkbox.html diff --git a/rest_framework/templates/rest_framework/fields/horizontal/fieldset.html b/rest_framework/templates/rest_framework/horizontal/fields/fieldset.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/horizontal/fieldset.html rename to rest_framework/templates/rest_framework/horizontal/fields/fieldset.html diff --git a/rest_framework/templates/rest_framework/fields/horizontal/input.html b/rest_framework/templates/rest_framework/horizontal/fields/input.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/horizontal/input.html rename to rest_framework/templates/rest_framework/horizontal/fields/input.html diff --git a/rest_framework/templates/rest_framework/fields/horizontal/list_fieldset.html b/rest_framework/templates/rest_framework/horizontal/fields/list_fieldset.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/horizontal/list_fieldset.html rename to rest_framework/templates/rest_framework/horizontal/fields/list_fieldset.html diff --git a/rest_framework/templates/rest_framework/fields/horizontal/select.html b/rest_framework/templates/rest_framework/horizontal/fields/select.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/horizontal/select.html rename to rest_framework/templates/rest_framework/horizontal/fields/select.html diff --git a/rest_framework/templates/rest_framework/fields/horizontal/select_checkbox.html b/rest_framework/templates/rest_framework/horizontal/fields/select_checkbox.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/horizontal/select_checkbox.html rename to rest_framework/templates/rest_framework/horizontal/fields/select_checkbox.html diff --git a/rest_framework/templates/rest_framework/fields/horizontal/select_multiple.html b/rest_framework/templates/rest_framework/horizontal/fields/select_multiple.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/horizontal/select_multiple.html rename to rest_framework/templates/rest_framework/horizontal/fields/select_multiple.html diff --git a/rest_framework/templates/rest_framework/fields/horizontal/select_radio.html b/rest_framework/templates/rest_framework/horizontal/fields/select_radio.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/horizontal/select_radio.html rename to rest_framework/templates/rest_framework/horizontal/fields/select_radio.html diff --git a/rest_framework/templates/rest_framework/fields/horizontal/textarea.html b/rest_framework/templates/rest_framework/horizontal/fields/textarea.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/horizontal/textarea.html rename to rest_framework/templates/rest_framework/horizontal/fields/textarea.html diff --git a/rest_framework/templates/rest_framework/horizontal/form.html b/rest_framework/templates/rest_framework/horizontal/form.html new file mode 100644 index 000000000..7560d8a26 --- /dev/null +++ b/rest_framework/templates/rest_framework/horizontal/form.html @@ -0,0 +1,15 @@ +{% load rest_framework %} +
+ {% csrf_token %} + {% for field in form %} + {% if not field.read_only %} + {% render_field field template_pack=template_pack renderer=renderer %} + {% endif %} + {% endfor %} + +
+
+ +
+
+
diff --git a/rest_framework/templates/rest_framework/fields/inline/checkbox.html b/rest_framework/templates/rest_framework/inline/fields/checkbox.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/inline/checkbox.html rename to rest_framework/templates/rest_framework/inline/fields/checkbox.html diff --git a/rest_framework/templates/rest_framework/fields/inline/fieldset.html b/rest_framework/templates/rest_framework/inline/fields/fieldset.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/inline/fieldset.html rename to rest_framework/templates/rest_framework/inline/fields/fieldset.html diff --git a/rest_framework/templates/rest_framework/fields/inline/input.html b/rest_framework/templates/rest_framework/inline/fields/input.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/inline/input.html rename to rest_framework/templates/rest_framework/inline/fields/input.html diff --git a/rest_framework/templates/rest_framework/fields/inline/select.html b/rest_framework/templates/rest_framework/inline/fields/select.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/inline/select.html rename to rest_framework/templates/rest_framework/inline/fields/select.html diff --git a/rest_framework/templates/rest_framework/fields/inline/select_checkbox.html b/rest_framework/templates/rest_framework/inline/fields/select_checkbox.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/inline/select_checkbox.html rename to rest_framework/templates/rest_framework/inline/fields/select_checkbox.html diff --git a/rest_framework/templates/rest_framework/fields/inline/select_multiple.html b/rest_framework/templates/rest_framework/inline/fields/select_multiple.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/inline/select_multiple.html rename to rest_framework/templates/rest_framework/inline/fields/select_multiple.html diff --git a/rest_framework/templates/rest_framework/fields/inline/select_radio.html b/rest_framework/templates/rest_framework/inline/fields/select_radio.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/inline/select_radio.html rename to rest_framework/templates/rest_framework/inline/fields/select_radio.html diff --git a/rest_framework/templates/rest_framework/inline/fields/textarea.html b/rest_framework/templates/rest_framework/inline/fields/textarea.html new file mode 100644 index 000000000..bdf25ffe6 --- /dev/null +++ b/rest_framework/templates/rest_framework/inline/fields/textarea.html @@ -0,0 +1,6 @@ +
+ {% if field.label %} + + {% endif %} + +
diff --git a/rest_framework/templates/rest_framework/inline/form.html b/rest_framework/templates/rest_framework/inline/form.html new file mode 100644 index 000000000..b13d16614 --- /dev/null +++ b/rest_framework/templates/rest_framework/inline/form.html @@ -0,0 +1,11 @@ +{% load rest_framework %} +
+ {% csrf_token %} + {% for field in form %} + {% if not field.read_only %} + {% render_field field template_pack=template_pack renderer=renderer %} + {% endif %} + {% endfor %} + + +
diff --git a/rest_framework/templates/rest_framework/fields/vertical/checkbox.html b/rest_framework/templates/rest_framework/vertical/fields/checkbox.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/vertical/checkbox.html rename to rest_framework/templates/rest_framework/vertical/fields/checkbox.html diff --git a/rest_framework/templates/rest_framework/fields/vertical/fieldset.html b/rest_framework/templates/rest_framework/vertical/fields/fieldset.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/vertical/fieldset.html rename to rest_framework/templates/rest_framework/vertical/fields/fieldset.html diff --git a/rest_framework/templates/rest_framework/fields/vertical/input.html b/rest_framework/templates/rest_framework/vertical/fields/input.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/vertical/input.html rename to rest_framework/templates/rest_framework/vertical/fields/input.html diff --git a/rest_framework/templates/rest_framework/fields/vertical/list_fieldset.html b/rest_framework/templates/rest_framework/vertical/fields/list_fieldset.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/vertical/list_fieldset.html rename to rest_framework/templates/rest_framework/vertical/fields/list_fieldset.html diff --git a/rest_framework/templates/rest_framework/fields/vertical/select.html b/rest_framework/templates/rest_framework/vertical/fields/select.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/vertical/select.html rename to rest_framework/templates/rest_framework/vertical/fields/select.html diff --git a/rest_framework/templates/rest_framework/fields/vertical/select_checkbox.html b/rest_framework/templates/rest_framework/vertical/fields/select_checkbox.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/vertical/select_checkbox.html rename to rest_framework/templates/rest_framework/vertical/fields/select_checkbox.html diff --git a/rest_framework/templates/rest_framework/fields/vertical/select_multiple.html b/rest_framework/templates/rest_framework/vertical/fields/select_multiple.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/vertical/select_multiple.html rename to rest_framework/templates/rest_framework/vertical/fields/select_multiple.html diff --git a/rest_framework/templates/rest_framework/fields/vertical/select_radio.html b/rest_framework/templates/rest_framework/vertical/fields/select_radio.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/vertical/select_radio.html rename to rest_framework/templates/rest_framework/vertical/fields/select_radio.html diff --git a/rest_framework/templates/rest_framework/fields/vertical/textarea.html b/rest_framework/templates/rest_framework/vertical/fields/textarea.html similarity index 100% rename from rest_framework/templates/rest_framework/fields/vertical/textarea.html rename to rest_framework/templates/rest_framework/vertical/fields/textarea.html diff --git a/rest_framework/templates/rest_framework/vertical/form.html b/rest_framework/templates/rest_framework/vertical/form.html new file mode 100644 index 000000000..fe908af02 --- /dev/null +++ b/rest_framework/templates/rest_framework/vertical/form.html @@ -0,0 +1,11 @@ +{% load rest_framework %} +
+ {% csrf_token %} + {% for field in form %} + {% if not field.read_only %} + {% render_field field template_pack=template_pack renderer=renderer %} + {% endif %} + {% endfor %} + + +
diff --git a/rest_framework/templatetags/rest_framework.py b/rest_framework/templatetags/rest_framework.py index 49a4c3389..c092d39f6 100644 --- a/rest_framework/templatetags/rest_framework.py +++ b/rest_framework/templatetags/rest_framework.py @@ -32,8 +32,8 @@ class_re = re.compile(r'(?<=class=["\'])(.*)(?=["\'])') # And the template tags themselves... @register.simple_tag -def render_field(field, layout=None, renderer=None): - return renderer.render_field(field, layout) +def render_field(field, template_pack=None, renderer=None): + return renderer.render_field(field, template_pack) @register.simple_tag