Django template in static directory?

489 views Asked by At

It is a good practice to put my django templates in my static directory?

The collectstatic would copy all my template files in the static directory anyway. Why not putting the static files in the static dir directly?

2

There are 2 answers

1
sthzg On BEST ANSWER

Is it a good practice to put my django templates in my static directory?

Well, generally no, because it makes them callable from outside (by directly accessing http://example.com/static/templates/base.html) and may reveal some of your internal data structure by exposing the template tags and context variables. However, sometimes this is used to provide templates for Javascript frameworks in an HTML5 app context.

Templates exposed in that HTML5 app context however don't make use of any of Django's template layer and for use cases like that it's arguably a better solution to completely separate frontend and backend. But use cases and implementations vary in that domain.

The collectstatic would copy all my template files in the static directory anyway. Why not putting the static files in the static dir directly?

No, that would only happen if you store them under the static directory of your app, which would be a wrong habit (again if not specifically chosen as a way to expose a template to a Javascript app). If you store them at templates/myapp they will not get copied over. Also it wouldn't work out of the box as Django does not look into STATIC_ROOT for templates by default.

0
Daniel Roseman On

No, not at all.

Templates are not static files. collectstatic does not copy your template files into the static directory. There is no reason to put them there.