Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Customize base template #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ docs/build
neapolitan.code-workspace
.coverage
dist
.DS_Store
5 changes: 3 additions & 2 deletions docs/source/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ Finally, start the runserver and in the admin, add a few ``Project`` objects to
Wire up Neapolitan views
------------------------

Neapolitan expects to extend a base template (its own templates use
``{% extends "base.html" %}`` so you'll have to provide one at ``dashboard/templates/base.html``::
Neapolitan expects to extend a base template called ``base.html``.
So you'll have to provide one at ``dashboard/templates/base.html`` or override the base template by defining a class attribute called ``base_template`` in your view classes.
The base template needs to have a block called ``content``::

{% block content %}{% endblock %}

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
7 changes: 7 additions & 0 deletions src/neapolitan/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class CRUDView(View):
CRUDView is Neapolitan's core. It provides the standard list, detail,
create, edit, and delete views for a model, as well as the hooks you need to
be able to customise any part of that.

Following attributes can be overridden by defining in extending view classes or by passing to as_view():

* base_template: Base template that Neapolitan extends from. If not defined defaults to ``base.html``.
"""

role: Role
Expand Down Expand Up @@ -95,6 +99,8 @@ class CRUDView(View):
# Suffix that should be appended to automatically generated template names.
template_name_suffix = None

base_template = "base.html"

# Filtering.

def get_filterset(self, queryset=None):
Expand Down Expand Up @@ -248,6 +254,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.base_template

if getattr(self, "object", None) is not None:
kwargs["object"] = self.object
Expand Down
28 changes: 28 additions & 0 deletions tests/migrations/0002_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 4.2.2 on 2023-06-20 11:53

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("tests", "0001_initial"),
]

operations = [
migrations.CreateModel(
name="Project",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=25)),
],
),
]
4 changes: 4 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ class Bookmark(models.Model):
title = models.CharField(max_length=255)
note = models.TextField(blank=True)
favourite = models.BooleanField(default=False)


class Project(models.Model):
name = models.CharField(max_length=25)
9 changes: 9 additions & 0 deletions tests/templates/custom_base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Custom Base Template</title>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
16 changes: 14 additions & 2 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from neapolitan.views import CRUDView

from .models import Bookmark
from .models import Bookmark, Project


class BookmarkView(CRUDView):
Expand All @@ -15,7 +15,13 @@ class BookmarkView(CRUDView):
]


urlpatterns = [] + BookmarkView.get_urls()
class ProjectView(CRUDView):
model = Project
fields = ["name"]
base_template = "custom_base.html"


urlpatterns = [] + BookmarkView.get_urls() + ProjectView.get_urls()


class BasicTests(TestCase):
Expand Down Expand Up @@ -122,3 +128,9 @@ def test_filter(self):
self.assertContains(response, self.homepage.title)
self.assertNotContains(response, self.github.title)
self.assertNotContains(response, self.fosstodon.title)

def test_custom_base_template(self):
"""Test that base template can be customized per view"""
response = self.client.get("/project/")
self.assertEqual(response.status_code, 200)
self.assertContains(response, "<title>Custom Base Template</title>")