From e0f7d2cd9f9ef1f9788d402b6a2861946d4c2fc9 Mon Sep 17 00:00:00 2001 From: Paul Oswald Date: Wed, 15 Feb 2012 10:21:02 +0900 Subject: [PATCH 1/4] Add empty {% blocks %} to the template to give people a chance to override --- .../templates/base_renderer.html | 142 ++++++++++++++++++ djangorestframework/templates/renderer.html | 130 +--------------- 2 files changed, 144 insertions(+), 128 deletions(-) create mode 100644 djangorestframework/templates/base_renderer.html diff --git a/djangorestframework/templates/base_renderer.html b/djangorestframework/templates/base_renderer.html new file mode 100644 index 000000000..1d4ae92ae --- /dev/null +++ b/djangorestframework/templates/base_renderer.html @@ -0,0 +1,142 @@ + + +{% load urlize_quoted_links %} +{% load add_query_param %} +{% load static %} + + + + {% block extrastyle %}{% endblock %} + {% block title %}Django REST framework - {{ name }}{% endblock %} + {% block extrahead %}{% endblock %} + {% block blockbots %}{% endblock %} + + +
+ + + + + + +
+ + {% if 'OPTIONS' in view.allowed_methods %} +
+ {% csrf_token %} + + +
+ {% endif %} + +
+

{{ name }}

+

{{ description }}

+
+
{{ response.status }} {{ response.status_text }}{% autoescape off %}
+{% for key, val in response.headers.items %}{{ key }}: {{ val|urlize_quoted_links }}
+{% endfor %}
+{{ content|urlize_quoted_links }}
{% endautoescape %}
+ + {% if 'GET' in view.allowed_methods %} +
+
+

GET {{ name }}

+
+ GET + {% for format in available_formats %} + {% with FORMAT_PARAM|add:"="|add:format as param %} + [{{ format }}] + {% endwith %} + {% endfor %} +
+
+
+ {% endif %} + + {# Only display the POST/PUT/DELETE forms if method tunneling via POST forms is enabled and the user has permissions on this view. #} + {% if METHOD_PARAM and response.status != 403 %} + + {% if 'POST' in view.allowed_methods %} +
+
+

POST {{ name }}

+ {% csrf_token %} + {{ post_form.non_field_errors }} + {% for field in post_form %} +
+ {{ field.label_tag }} + {{ field }} + {{ field.help_text }} + {{ field.errors }} +
+ {% endfor %} +
+ +
+
+
+ {% endif %} + + {% if 'PUT' in view.allowed_methods %} +
+
+

PUT {{ name }}

+ + {% csrf_token %} + {{ put_form.non_field_errors }} + {% for field in put_form %} +
+ {{ field.label_tag }} + {{ field }} + {{ field.help_text }} + {{ field.errors }} +
+ {% endfor %} +
+ +
+
+
+ {% endif %} + + {% if 'DELETE' in view.allowed_methods %} +
+
+

DELETE {{ name }}

+ {% csrf_token %} + +
+ +
+
+
+ {% endif %} + + {% endif %} +
+ + +
+ + + {% block footer %}{% endblock %} +
+ + diff --git a/djangorestframework/templates/renderer.html b/djangorestframework/templates/renderer.html index bda49e6f8..adb42c7f2 100644 --- a/djangorestframework/templates/renderer.html +++ b/djangorestframework/templates/renderer.html @@ -1,129 +1,3 @@ - - -{% load urlize_quoted_links %} -{% load add_query_param %} -{% load static %} - - - - Django REST framework - {{ name }} - - -
+{% extends "base_renderer.html" %} - - - - -
- - {% if 'OPTIONS' in view.allowed_methods %} -
- {% csrf_token %} - - -
- {% endif %} - -
-

{{ name }}

-

{{ description }}

-
-
{{ response.status }} {{ response.status_text }}{% autoescape off %}
-{% for key, val in response.headers.items %}{{ key }}: {{ val|urlize_quoted_links }}
-{% endfor %}
-{{ content|urlize_quoted_links }}
{% endautoescape %}
- - {% if 'GET' in view.allowed_methods %} -
-
-

GET {{ name }}

-
- GET - {% for format in available_formats %} - {% with FORMAT_PARAM|add:"="|add:format as param %} - [{{ format }}] - {% endwith %} - {% endfor %} -
-
-
- {% endif %} - - {# Only display the POST/PUT/DELETE forms if method tunneling via POST forms is enabled and the user has permissions on this view. #} - {% if METHOD_PARAM and response.status != 403 %} - - {% if 'POST' in view.allowed_methods %} -
-
-

POST {{ name }}

- {% csrf_token %} - {{ post_form.non_field_errors }} - {% for field in post_form %} -
- {{ field.label_tag }} - {{ field }} - {{ field.help_text }} - {{ field.errors }} -
- {% endfor %} -
- -
-
-
- {% endif %} - - {% if 'PUT' in view.allowed_methods %} -
-
-

PUT {{ name }}

- - {% csrf_token %} - {{ put_form.non_field_errors }} - {% for field in put_form %} -
- {{ field.label_tag }} - {{ field }} - {{ field.help_text }} - {{ field.errors }} -
- {% endfor %} -
- -
-
-
- {% endif %} - - {% if 'DELETE' in view.allowed_methods %} -
-
-

DELETE {{ name }}

- {% csrf_token %} - -
- -
-
-
- {% endif %} - - {% endif %} -
-
-
- - +{# Override this template in your own templates directory to customize #} \ No newline at end of file From cbd0752740b4dfb054179f68ca330b8ac212ec77 Mon Sep 17 00:00:00 2001 From: Paul Oswald Date: Wed, 15 Feb 2012 10:30:56 +0900 Subject: [PATCH 2/4] Move the templates into a named directory --- djangorestframework/renderers.py | 6 +++--- .../templates/{ => djangorestframework}/api_login.html | 0 .../templates/{ => djangorestframework}/base_renderer.html | 0 .../templates/{ => djangorestframework}/renderer.html | 2 +- .../templates/{ => djangorestframework}/renderer.txt | 0 djangorestframework/utils/staticviews.py | 4 ++-- 6 files changed, 6 insertions(+), 6 deletions(-) rename djangorestframework/templates/{ => djangorestframework}/api_login.html (100%) rename djangorestframework/templates/{ => djangorestframework}/base_renderer.html (100%) rename djangorestframework/templates/{ => djangorestframework}/renderer.html (57%) rename djangorestframework/templates/{ => djangorestframework}/renderer.txt (100%) diff --git a/djangorestframework/renderers.py b/djangorestframework/renderers.py index bb0f789aa..33c549fde 100644 --- a/djangorestframework/renderers.py +++ b/djangorestframework/renderers.py @@ -373,7 +373,7 @@ class DocumentingHTMLRenderer(DocumentingTemplateRenderer): media_type = 'text/html' format = 'html' - template = 'renderer.html' + template = 'djangorestframework/renderer.html' class DocumentingXHTMLRenderer(DocumentingTemplateRenderer): @@ -385,7 +385,7 @@ class DocumentingXHTMLRenderer(DocumentingTemplateRenderer): media_type = 'application/xhtml+xml' format = 'xhtml' - template = 'renderer.html' + template = 'djangorestframework/renderer.html' class DocumentingPlainTextRenderer(DocumentingTemplateRenderer): @@ -397,7 +397,7 @@ class DocumentingPlainTextRenderer(DocumentingTemplateRenderer): media_type = 'text/plain' format = 'txt' - template = 'renderer.txt' + template = 'djangorestframework/renderer.txt' DEFAULT_RENDERERS = ( diff --git a/djangorestframework/templates/api_login.html b/djangorestframework/templates/djangorestframework/api_login.html similarity index 100% rename from djangorestframework/templates/api_login.html rename to djangorestframework/templates/djangorestframework/api_login.html diff --git a/djangorestframework/templates/base_renderer.html b/djangorestframework/templates/djangorestframework/base_renderer.html similarity index 100% rename from djangorestframework/templates/base_renderer.html rename to djangorestframework/templates/djangorestframework/base_renderer.html diff --git a/djangorestframework/templates/renderer.html b/djangorestframework/templates/djangorestframework/renderer.html similarity index 57% rename from djangorestframework/templates/renderer.html rename to djangorestframework/templates/djangorestframework/renderer.html index adb42c7f2..01ca55435 100644 --- a/djangorestframework/templates/renderer.html +++ b/djangorestframework/templates/djangorestframework/renderer.html @@ -1,3 +1,3 @@ -{% extends "base_renderer.html" %} +{% extends "djangorestframework/base_renderer.html" %} {# Override this template in your own templates directory to customize #} \ No newline at end of file diff --git a/djangorestframework/templates/renderer.txt b/djangorestframework/templates/djangorestframework/renderer.txt similarity index 100% rename from djangorestframework/templates/renderer.txt rename to djangorestframework/templates/djangorestframework/renderer.txt diff --git a/djangorestframework/utils/staticviews.py b/djangorestframework/utils/staticviews.py index 9bae0ee78..caa63ccd3 100644 --- a/djangorestframework/utils/staticviews.py +++ b/djangorestframework/utils/staticviews.py @@ -12,7 +12,7 @@ import base64 # be making settings changes in order to accomodate django-rest-framework @csrf_protect @never_cache -def api_login(request, template_name='api_login.html', +def api_login(request, template_name='djangorestframework/api_login.html', redirect_field_name=REDIRECT_FIELD_NAME, authentication_form=AuthenticationForm): """Displays the login form and handles the login action.""" @@ -57,5 +57,5 @@ def api_login(request, template_name='api_login.html', }, context_instance=RequestContext(request)) -def api_logout(request, next_page=None, template_name='api_login.html', redirect_field_name=REDIRECT_FIELD_NAME): +def api_logout(request, next_page=None, template_name='djangorestframework/api_login.html', redirect_field_name=REDIRECT_FIELD_NAME): return logout(request, next_page, template_name, redirect_field_name) From bf717eff6420af78a41e1359cdd7b9e409076281 Mon Sep 17 00:00:00 2001 From: Paul Oswald Date: Wed, 15 Feb 2012 10:31:56 +0900 Subject: [PATCH 3/4] Documentation on how to override the template; Closes Issue #165 --- docs/howto/setup.rst | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/howto/setup.rst b/docs/howto/setup.rst index 22f98f0c6..d018c72d4 100644 --- a/docs/howto/setup.rst +++ b/docs/howto/setup.rst @@ -29,6 +29,19 @@ but once you move onto a production server, you'll want to make sure you serve t * Ensure that the ``ADMIN_MEDIA_PREFIX`` is set appropriately and that you are serving the admin media. (Django's testserver will automatically serve the admin media for you) +You may customize the templates by creating a new template called ``djangorestframework/renderer.html`` +in your project, extend ``djangorestframework/base_renderer.html`` and override the +appropriate ``{% block tags %}``. For example:: + + {% extends "djangorestframework/base_renderer.html" %} + + {% block title %}My API{% endblock %} + + {% block branding %} +

My API

+ {% endblock %} + + Markdown -------- From 50198935af1f772835a3c26943cbb245295c60e5 Mon Sep 17 00:00:00 2001 From: Paul Oswald Date: Mon, 20 Feb 2012 22:16:51 +0900 Subject: [PATCH 4/4] Rename templates to 'base.html', 'api.html', 'api.txt' and 'login.html' --- djangorestframework/renderers.py | 6 +++--- .../djangorestframework/{renderer.html => api.html} | 2 +- .../templates/djangorestframework/{renderer.txt => api.txt} | 0 .../djangorestframework/{base_renderer.html => base.html} | 0 .../djangorestframework/{api_login.html => login.html} | 0 djangorestframework/utils/staticviews.py | 4 ++-- docs/howto/setup.rst | 6 +++--- 7 files changed, 9 insertions(+), 9 deletions(-) rename djangorestframework/templates/djangorestframework/{renderer.html => api.html} (57%) rename djangorestframework/templates/djangorestframework/{renderer.txt => api.txt} (100%) rename djangorestframework/templates/djangorestframework/{base_renderer.html => base.html} (100%) rename djangorestframework/templates/djangorestframework/{api_login.html => login.html} (100%) diff --git a/djangorestframework/renderers.py b/djangorestframework/renderers.py index 33c549fde..de9a01ec4 100644 --- a/djangorestframework/renderers.py +++ b/djangorestframework/renderers.py @@ -373,7 +373,7 @@ class DocumentingHTMLRenderer(DocumentingTemplateRenderer): media_type = 'text/html' format = 'html' - template = 'djangorestframework/renderer.html' + template = 'djangorestframework/api.html' class DocumentingXHTMLRenderer(DocumentingTemplateRenderer): @@ -385,7 +385,7 @@ class DocumentingXHTMLRenderer(DocumentingTemplateRenderer): media_type = 'application/xhtml+xml' format = 'xhtml' - template = 'djangorestframework/renderer.html' + template = 'djangorestframework/api.html' class DocumentingPlainTextRenderer(DocumentingTemplateRenderer): @@ -397,7 +397,7 @@ class DocumentingPlainTextRenderer(DocumentingTemplateRenderer): media_type = 'text/plain' format = 'txt' - template = 'djangorestframework/renderer.txt' + template = 'djangorestframework/api.txt' DEFAULT_RENDERERS = ( diff --git a/djangorestframework/templates/djangorestframework/renderer.html b/djangorestframework/templates/djangorestframework/api.html similarity index 57% rename from djangorestframework/templates/djangorestframework/renderer.html rename to djangorestframework/templates/djangorestframework/api.html index 01ca55435..fd9bcc983 100644 --- a/djangorestframework/templates/djangorestframework/renderer.html +++ b/djangorestframework/templates/djangorestframework/api.html @@ -1,3 +1,3 @@ -{% extends "djangorestframework/base_renderer.html" %} +{% extends "djangorestframework/base.html" %} {# Override this template in your own templates directory to customize #} \ No newline at end of file diff --git a/djangorestframework/templates/djangorestframework/renderer.txt b/djangorestframework/templates/djangorestframework/api.txt similarity index 100% rename from djangorestframework/templates/djangorestframework/renderer.txt rename to djangorestframework/templates/djangorestframework/api.txt diff --git a/djangorestframework/templates/djangorestframework/base_renderer.html b/djangorestframework/templates/djangorestframework/base.html similarity index 100% rename from djangorestframework/templates/djangorestframework/base_renderer.html rename to djangorestframework/templates/djangorestframework/base.html diff --git a/djangorestframework/templates/djangorestframework/api_login.html b/djangorestframework/templates/djangorestframework/login.html similarity index 100% rename from djangorestframework/templates/djangorestframework/api_login.html rename to djangorestframework/templates/djangorestframework/login.html diff --git a/djangorestframework/utils/staticviews.py b/djangorestframework/utils/staticviews.py index caa63ccd3..7cbc0b9b8 100644 --- a/djangorestframework/utils/staticviews.py +++ b/djangorestframework/utils/staticviews.py @@ -12,7 +12,7 @@ import base64 # be making settings changes in order to accomodate django-rest-framework @csrf_protect @never_cache -def api_login(request, template_name='djangorestframework/api_login.html', +def api_login(request, template_name='djangorestframework/login.html', redirect_field_name=REDIRECT_FIELD_NAME, authentication_form=AuthenticationForm): """Displays the login form and handles the login action.""" @@ -57,5 +57,5 @@ def api_login(request, template_name='djangorestframework/api_login.html', }, context_instance=RequestContext(request)) -def api_logout(request, next_page=None, template_name='djangorestframework/api_login.html', redirect_field_name=REDIRECT_FIELD_NAME): +def api_logout(request, next_page=None, template_name='djangorestframework/login.html', redirect_field_name=REDIRECT_FIELD_NAME): return logout(request, next_page, template_name, redirect_field_name) diff --git a/docs/howto/setup.rst b/docs/howto/setup.rst index d018c72d4..64b58262c 100644 --- a/docs/howto/setup.rst +++ b/docs/howto/setup.rst @@ -29,11 +29,11 @@ but once you move onto a production server, you'll want to make sure you serve t * Ensure that the ``ADMIN_MEDIA_PREFIX`` is set appropriately and that you are serving the admin media. (Django's testserver will automatically serve the admin media for you) -You may customize the templates by creating a new template called ``djangorestframework/renderer.html`` -in your project, extend ``djangorestframework/base_renderer.html`` and override the +You may customize the templates by creating a new template called ``djangorestframework/api.html`` +in your project, extend ``djangorestframework/base.html`` and override the appropriate ``{% block tags %}``. For example:: - {% extends "djangorestframework/base_renderer.html" %} + {% extends "djangorestframework/base.html" %} {% block title %}My API{% endblock %}