简体   繁体   中英

Django template importing when creating a package

I'm create a new package, something very simple. I'm confused tho, how should I be doing templates?

You see I never know what project this application gets installed on ie pip install django-something , but I would like the 'something app' to inherent the template of the project its used on.

Is this my job, how do other do this?

Put the template in a location where the loaders configured in the project will find before they start looking in the app directories.

EG

If you have:

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

TEMPLATE_DIRS = 'myproject/templates'

and the app looks for foo/bar.html , then you just need to create myproject/templates/foo/bar.html in order to override the built-in template.

The trick is to "namespace" your own app's templates.
Instead of storing your app's templates in yourapp/templates/foo.html , add another directory inside your templates directory: yourapp/templates/yourapp/foo.html .

This means you can distinguish between your app's base.html and the project's base.html .

In your app's templates (for example, 'foo.html'), you inherit your app's base template:

{% extends "yourapp/base.html" %}`. 

You can then inherit from the project's base.html (if it exists), by starting it with:

{% extends "base.html" %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM