Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add ability to change base html template #21

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/source/templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ The templates ``{% extends "base.html" %}``, which must provide
``{% block content %}``. Neapolitan may provide a base template in the future,
see `issue #6 <https://github.com/carltongibson/neapolitan/issues/6>`_.

You can change the base html template that neapolitan looks for by setting the
``BASE_TEMPLATE`` in Neapolitan setting.

.. code-block:: html+django

NEAPOLITAN = {
"BASE_TEMPLATE": "dashboard_base.html",
}

This is the full listing of the provided templates:

.. code-block:: shell
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "base.html" %}
{% extends base_template %}

{% block content %}

Expand Down
2 changes: 1 addition & 1 deletion src/neapolitan/templates/neapolitan/object_detail.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "base.html" %}
{% extends base_template %}
{% load neapolitan %}

{% block content %}
Expand Down
2 changes: 1 addition & 1 deletion src/neapolitan/templates/neapolitan/object_form.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "base.html" %}
{% extends base_template %}

{% block content %}

Expand Down
2 changes: 1 addition & 1 deletion src/neapolitan/templates/neapolitan/object_list.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "base.html" %}
{% extends base_template %}
{% load neapolitan %}

{% block content %}
Expand Down
1 change: 1 addition & 0 deletions src/neapolitan/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .common import get_settings
15 changes: 15 additions & 0 deletions src/neapolitan/utils/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.conf import settings


def get_settings():
"""
Returns neapolitan settings defined in settings file.

If not defined returns an empty dict.
"""
try:
neapolitan_settings = settings.NEAPOLITAN
except AttributeError:
return {}
else:
return neapolitan_settings
12 changes: 12 additions & 0 deletions src/neapolitan/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from django.views.generic import View
from django_filters.filterset import filterset_factory

from neapolitan.utils import get_settings


# A CRUDView is a view that can perform all the CRUD operations on a model. The
# `role` attribute determines which operations are available for a given
Expand Down Expand Up @@ -248,6 +250,7 @@ def get_context_data(self, **kwargs):
kwargs["object_verbose_name"] = self.model._meta.verbose_name
kwargs["object_verbose_name_plural"] = self.model._meta.verbose_name_plural
kwargs["create_view_url"] = reverse(f"{self.model._meta.model_name}-create")
kwargs["base_template"] = self.get_base_template_name()

if getattr(self, "object", None) is not None:
kwargs["object"] = self.object
Expand All @@ -263,6 +266,15 @@ def get_context_data(self, **kwargs):

return kwargs

def get_base_template_name(self):
"""
Get base html page name.

Looks at settings and if not defined return base.html as default.
"""
settings = get_settings()
return settings.get("BASE_TEMPLATE", "base.html")

def get_template_names(self):
"""
Returns a list of template names to use when rendering the response.
Expand Down