What is the best location to put templates in django project?

后端 未结 9 635
情歌与酒
情歌与酒 2020-12-07 13:29

What is the best location to put templates in django project?

9条回答
  •  温柔的废话
    2020-12-07 13:46

    Following up from Dominic and dlrust,

    We use a setuptools source distribution (sdist) to package our django project and apps to deploy in our different environments.

    We have found that the templates and static files need to be under the django application directories so that they can be packaged up by setuptools.

    For example, our template and static paths look like:

    PROJECT/APP/templates/APP/template.html
    PROJECT/APP/static/APP/my.js
    

    For this to work, the MANIFEST.in needs to be modified (see http://docs.python.org/distutils/sourcedist.html#the-manifest-in-template)

    An example of the MANIFEST.in:

    include setup.py
    recursive-include PROJECT *.txt *.html *.js
    recursive-include PROJECT *.css *.js *.png *.gif *.bmp *.ico *.jpg *.jpeg
    

    Also, you need to confirm in your django settings file that the app_directories loader is in your TEMPLATE_LOADERS. I think it's there by default in django 1.4.

    An example of the django settings template loaders:

    # List of callables that know how to import templates from various sources.
    TEMPLATE_LOADERS = (
        'django.template.loaders.filesystem.Loader',
        'django.template.loaders.app_directories.Loader',
    )
    

    Just in case you are wondering why we use sdists instead of just coping rsync files; it's part of our configuration management workflow where we have a single build tarball that is deployed with PIP unchanged into test, acceptance and production environments.

提交回复
热议问题