This commit is contained in:
Will Abbott 2024-07-19 10:14:53 +01:00
parent 4e7e7d62b5
commit d5651d8b9b
13 changed files with 43 additions and 1 deletions

View file

@ -39,6 +39,7 @@ INSTALLED_APPS = [
"django.contrib.messages",
"django.contrib.staticfiles",
"django_cotton",
"unspecified_app_directory",
]
MIDDLEWARE = [

View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class UnspecifiedAppDirectoryConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'unspecified_app_directory'

View file

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View file

@ -0,0 +1 @@
My template was not specified in settings!

View file

@ -0,0 +1 @@
<c-unspecified-component />

View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View file

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

View file

@ -13,6 +13,7 @@ from django.template import Template
from django.core.cache import cache
from django.template import Origin
from django.conf import settings
from django.apps import apps
from bs4 import BeautifulSoup, MarkupResemblesLocatorWarning
@ -60,7 +61,15 @@ class Loader(BaseLoader):
raise TemplateDoesNotExist(template_name)
def get_dirs(self):
return self.dirs if self.dirs is not None else self.engine.dirs
dirs = self.dirs if self.dirs is not None else self.engine.dirs
# Add app-specific template directories
for app_config in apps.get_app_configs():
template_dir = os.path.join(app_config.path, "templates")
if os.path.isdir(template_dir):
dirs.append(template_dir)
return dirs
def get_template_sources(self, template_name):
"""Return an Origin object pointing to an absolute path in each directory

View file

@ -352,3 +352,11 @@ class CottonTestCase(TestCase):
response,
"""attrs tag is: 'normal="normal" attr1="Hello Will" attr2="world" attr3="cowabonga!"'""",
)
def test_loader_scans_all_app_directories(self):
response = self.client.get("/test/unspecified-app-directory-template")
self.assertContains(
response,
"""My template was not specified in settings!""",
)

View file

@ -50,4 +50,8 @@ urlpatterns = [
"test/native-tags-in-attributes",
TemplateView.as_view(template_name="native_tags_in_attributes_view.html"),
),
path(
"test/unspecified-app-directory-template",
TemplateView.as_view(template_name="unspecified_view.html"),
),
]