开发者

Enabling overriding of app template in django?

I'm writing a simple site to display statistics for some data, and I've got an app called "stats", that I'd like to write default templates for (places in stats/templates/stats), but I'd like these to be overridable in the same way that the templates for the admin app are. IE: If I put a stats/view.html in my proje开发者_StackOverflowct's templates directory, it would be used rather than the one in my apps directory. I can't seem to quite figure out how to do this. How can I get Django to search the project's TEMPLATE_DIRS before it hits up the apps?

Edit: Found the problem, saw someone's tutorial for setting the template directories using the os module. They had:

TEMPLATE_DIRS = (
    os.path.join(os.path.basename(__file__), 'templates')
)

Which should be:

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), 'templates')
)


You need to put filesystem loader before app_directories loader in your TEMPLATE_LOADERS setting.

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.load_template_source',
    'django.template.loaders.app_directories.load_template_source'
)

The order of TEMPLATE_LOADERS matter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜