wrabit / django-cotton

Enabling Modern UI Composition in Django
https://django-cotton.com
MIT License
577 stars 24 forks source link

Changes to component files during development require server restart #85

Closed anze3db closed 2 months ago

anze3db commented 2 months ago

Hey! Thanks for this writing django-cotton, I really like how you implemented components!

I found an issue with the latest version. The dev server has to be restarted before changes to the component files take affect. I think this might be an unexpected side affect of https://github.com/wrabit/django-cotton/commit/748c4527c347402300ced9260486b7f7590d1992 since it doesn't reproduce with django-cotton<0.9.27.

To reproduce edit a component template and refresh the page. It won't load the new version until you restart the django dev server!

Thank you again for your work on this! 💚

wrabit commented 2 months ago

@anze3db thanks for the report and supporting cotton... I've reverted changes from the latest release until I can address the original issue.

wrabit commented 2 months ago

@anze3db just to let you know - I've just released a new version which simplifies the caching somewhat and now behaves more typically, clearing local cache on file changes. Please also note there is now an automatic configuration feature so be sure to read the release notes!

anze3db commented 2 months ago

@wrabit awesome, thank you for the heads up. I've tried it out and works great. 👍 I'm using SimpleAppConfig because I'm also using Carlton's template_partials in this project 😅

lcfd commented 2 months ago

@anze3db Hi! How do you have configured template_partials?

I'm getting:

TypeError: Loader.__init__() missing 1 required positional argument: 'loaders'

My config is:

INSTALLED_APPS += [
    "django_cotton.apps.SimpleAppConfig",
    "template_partials.apps.SimpleAppConfig",
]

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [
            BASE_DIR / "templates",
        ],
        "APP_DIRS": False,  # False because explicit declaration in loaders
        "NAME": "all",
        "OPTIONS": {
            "loaders": [
                (
                    "template_partials.loader.Loader",
                    (
                        "django.template.loaders.cached.Loader",
                        [
                            "django_cotton.cotton_loader.Loader",
                            "django.template.loaders.filesystem.Loader",
                            "django.template.loaders.app_directories.Loader",
                        ],
                    ),
                )
            ],
            "builtins": [
                "django_cotton.templatetags.cotton",
                "lucide.templatetags.lucide",
                "template_partials.templatetags.partials",
            ],
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]

@wrabit I think that the usage of template_partials by Carlton with Cotton will be really common. Can it be a good idea to create a dedicated "integration" guide?

anze3db commented 2 months ago

@lcfd this is my config: https://github.com/anze3db/fedidevs/blob/7d542951492bdbfb90426b71b1bb774ac7db42c3/fedidevs/settings.py#L104-L137

From what I can tell it's the same as yours, no idea why you're getting an error and I'm not 🤷

lcfd commented 2 months ago

For reference this is the error I get once I add template_partials:

Watching for file changes with StatReloader
Performing system checks...

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File ".venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_
from_command_line
    utility.execute()
  File ".venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File ".venv/lib/python3.12/site-packages/django/core/management/base.py", line 413, in run_from_arg
v
    self.execute(*args, **cmd_options)
  File ".venv/lib/python3.12/site-packages/django/core/management/commands/runserver.py", line 75, in
 execute
    super().execute(*args, **options)
  File ".venv/lib/python3.12/site-packages/django/core/management/base.py", line 459, in execute
System check identified some issues:

WARNINGS:
?: (debug_toolbar.W006) At least one DjangoTemplates TEMPLATES configuration needs to use django.template.loaders.app_directories.Loader or ha
ve APP_DIRS set to True.
        HINT: Include django.template.loaders.app_directories.Loader in ["OPTIONS"]["loaders"]. Alternatively use APP_DIRS=True for at least o
ne django.template.backends.django.DjangoTemplates backend configuration.

System check identified 1 issue (0 silenced).
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/django/core/management/commands/runserver.py", line 112, i
n handle
    self.run(**options)
  File ".venv/lib/python3.12/site-packages/django/core/management/commands/runserver.py", line 119, i
n run
    autoreload.run_with_reloader(self.inner_run, **options)
  File ".venv/lib/python3.12/site-packages/django/utils/autoreload.py", line 671, in run_with_reloade
r
    start_django(reloader, main_func, *args, **kwargs)
  File ".venv/lib/python3.12/site-packages/django/utils/autoreload.py", line 660, in start_django
    reloader.run(django_main_thread)
  File ".venv/lib/python3.12/site-packages/django/utils/autoreload.py", line 343, in run
    autoreload_started.send(sender=self)
  File ".venv/lib/python3.12/site-packages/django/dispatch/dispatcher.py", line 189, in send
    response = receiver(signal=self, sender=sender, **named)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/django/template/autoreload.py", line 50, in watch_for_temp
late_changes
    for directory in get_template_directories():
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/django/template/autoreload.py", line 22, in get_template_d
irectories
    for loader in backend.engine.template_loaders:
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/django/utils/functional.py", line 47, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
                                         ^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/django/template/engine.py", line 131, in template_loaders
    return self.get_template_loaders(self.loaders)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/django/template/engine.py", line 136, in get_template_loaders
    loader = self.find_template_loader(template_loader)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/django/template/engine.py", line 149, in find_template_loader
    return loader_class(self, *args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/template_partials/loader.py", line 16, in __init__
    self.loaders = engine.get_template_loaders(loaders)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/django/template/engine.py", line 136, in get_template_loaders
    loader = self.find_template_loader(template_loader)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/django/template/engine.py", line 149, in find_template_loader
    return loader_class(self, *args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Loader.__init__() missing 1 required positional argument: 'loaders'
Python 3.12 lib ver
asgiref 3.8.1
attrs 24.2.0
beautifulsoup4 4.12.3
certifi 2024.8.30
cffi 1.17.0
charset-normalizer 3.3.2
click 8.1.7
colorama 0.4.6
cryptography 43.0.0
curlylint 0.13.1
Django 5.1
django-allauth 64.2.0
django-browser-reload 1.15.0
django-cotton 0.9.32
django-debug-toolbar 4.4.6
django-htmx 1.19.0
django-js-asset 2.2.0
django-mailbox 4.10.1
django-prose-editor 0.8.0
django-stubs 5.0.4
django-stubs-ext 5.0.4
django-tailwind 3.8.0
django-template-partials 24.4
django-widget-tweaks 1.5.0
djhtml 3.0.6
idna 3.8
iniconfig 2.0.0
lucide 1.0.0
mypy 1.11.2
mypy-extensions 1.0.0
oauthlib 3.2.2
packaging 24.1
parsy 1.1.0
pathspec 0.12.1
pillow 10.4.0
pip 24.2
pluggy 1.5.0
pycparser 2.22
PyJWT 2.9.0
pytest 8.3.2
requests 2.32.3
requests-oauthlib 2.0.0
soupsieve 2.6
sqlparse 0.5.1
toml 0.10.2
types-PyYAML 6.0.12.20240808
typing_extensions 4.12.2
urllib3 2.2.2
anze3db commented 2 months ago

I really don't see what could be causing this. Can you reproduce it in an empty Django project? If we could do a git checkout and reproduce it locally it will be much easier to debug.

lcfd commented 2 months ago

Update

For now, I've solved the problem by removing "django.template.loaders.cached.Loader".

"loaders": [
                (
                    "template_partials.loader.Loader",
                    # (
                        # "django.template.loaders.cached.Loader",
                        [
                            "django_cotton.cotton_loader.Loader",
                            "django.template.loaders.filesystem.Loader",
                            "django.template.loaders.app_directories.Loader",
                        ],
                    #),
                )
            ],
wrabit commented 1 month ago

@lcfd FY I just posted an update to docs to about setup with DTP - But I just noticed I am recommending the way that doesn't work for you.

If you're still looking for ability to use the django cache loader - I might suggest you try @copyfactory's suggestion!

alfonsrv commented 1 month ago

Just on a sidenote I am using https://github.com/adamchainz/django-browser-reload and am getting multiple reload triggers per HTML file save since I have introduced django-cotton yesterday.

Haven't looked into it further, but thought I might mention it. edit: Also, great package!