我想知道为通过 pip 安装的应用程序覆盖模板的正确方法是什么。我的示例与django-notifications应用程序有关。结构如下:
notifications
|---- __init__.py
|---- templates
|---- notifications
|---- list.html
|---- notice.html
我在我的应用程序中复制了这个结构并相应地修改了 .html 文件。但是现在我面临以下错误:ImportError: cannot import name notify
在我调用的另一个应用程序(配置文件)的视图中发生这种情况from notifications import notify
。
在不覆盖模板的情况下,我不会出错。我错过了什么?我需要添加到 settings.py 中吗?
(我完全按照包中的 README.md 安装说明进行操作,并且在不覆盖模板的情况下使其正常工作)
追溯
File "/projectpath/project/urls.py", line 13, in <module>
url(r'^profile/', include('profile.urls')),
File "/home/user/.virtualenvs/project/local/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 26, in include
urlconf_module = import_module(urlconf_module)
File "/home/user/.virtualenvs/project/local/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
__import__(name)
File "/projectpath/profile/urls.py", line 2, in <module>
from profile import views
File "/projectpath/profile/views.py", line 17, in <module>
from notifications import notify
ImportError: cannot import name notify
看法
from notifications import notify
def edit(request):
...
notify.send(request.user, recipient=request.user, verb='you reached level 10')
设置
# Project directory root assuming: yunite.settings.base
PROJECT_DIR = Path(__file__).ancestor(3)
#ROOT_PATH = os.path.dirname(__file__)
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.request",
"django.contrib.auth.context_processors.auth",
)
# Directory to find templates
TEMPLATE_DIRS = (
PROJECT_DIR.child("templates"),
)
INSTALLED_APPS = (
...
'notifications',
)