Question

I'm wondering what the proper way to override templates for an app installed via pip. My example pertains to the django-notifications app. The structure is as follows:

notifications
|---- __init__.py
|---- templates
      |---- notifications
            |---- list.html
            |---- notice.html

I have duplicated this structure in my app and modified the .html files accordingly. However now I am faced with the following error: ImportError: cannot import name notify which occurs when in a view from another app (profile) where I call from notifications import notify.

Without overriding the templates I get no errors. What am I missing? Is there something additional I need to add to the settings.py?

(I have followed the README.md Installation Instructions in the package exactly and have it working properly without overriding templates)

Trackback

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

View

from notifications import notify

def edit(request):
  ...
  notify.send(request.user, recipient=request.user, verb='you reached level 10')

Settings

# 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',
)
Was it helpful?

Solution

Overriding a template in Django is simple. In your main application directory, there must be a templates directory (create if not already). Then create a new directory inside the template directory, which is named (notifications) in your case. Then copy the template files here (list.html and notice.html) in your case.

When done, you have overridden the app templates with your own templates. Feel free to edit them.

OTHER TIPS

Sorry for asking: did you add "notifications" to the INSTALLED_APPS? This sounds like the overridden template just makes the import error visible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top