carltongibson / neapolitan

Quick CRUD views for Django
https://noumenal.es/neapolitan/
MIT License
480 stars 33 forks source link

mktemplate does not work with project level template directories #48

Open haukuri opened 3 months ago

haukuri commented 3 months ago

When trying to generate templates with the mktemplate management command in a project with a project-level template directory, the command blows up when trying to access the template_dirs attribute on an instance of django.template.engine.Engine.

File "/home/haukur/Projects/neapolitan/src/neapolitan/management/commands/mktemplate.py", line 100, in handle
    target_dir = Engine.get_default().template_dirs[0]
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Engine' object has no attribute 'template_dirs'. Did you mean: 'template_loaders'?

I went digging around and found that template_dirs is an attribute of django.template.backends.base.BaseEngine.

I can only assume that there are some cases in which Engine.get_default() returns a subclass of BaseEngine instead of an instance of Engine.

I have a test project in a fork of Neapolitan that can be used to reproduce the error with

python manage.py mktemplate ice_cream.IceCream --list

I also have a branch in which I patched the mktemplate handle method to use the dirs property on Engine instead of the template_dirs property on BaseEngine if the latter is not present on the default engine. I'm not sure if that is always the correct behavior but it works on my machine (tm).

carltongibson commented 3 months ago

Yes, this would be an addition. I only use app template dirs, so never built for this.

Happy to take a patch adding a fallback with tests.

jefftriplett commented 2 months ago

I just tested this for a project-level template, and it worked great.

I did not test it for an app-level template.