thumbtackthief thumbtackthief - 4 months ago 15
Python Question

Customize templates in a third party Django app

I'm using a third party app (django-social-share) in my Django project but I need to customize the templates. I'm not sure how to go about doing that--everything I try keeps using the default templates.

The current default template is stored in:

django-social-share/django_social_share/templates/django_social_share/templatetags/post_to_facebook.html.


I've made my custom one in:

{{project_root}}/{{app_name}}/templates/django_social_share/templatetags/post_to_facebook.html


My templates setting:

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.core.context_processors.request',
'mainsite.context_processors.google_api'
],
'loaders': (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'django.template.loaders.eggs.Loader',
),
},
},
]

Answer

You can put the template in a separate app within your project. Django looks for templates in your apps in the order they are defined in INSTALLED_APPS, and uses the first match:

The order of INSTALLED_APPS is significant! For example, if you want to customize the Django admin, you might choose to override the standard admin/base_site.html template, from django.contrib.admin, with your own admin/base_site.html in myproject.polls. You must then make sure that your myproject.polls comes before django.contrib.admin in INSTALLED_APPS, otherwise django.contrib.admin’s will be loaded first and yours will be ignored. (source)

So create an app, called e.g. social_share_templates, and put it in INSTALLED_APPS before django_social_share. Then add the template to this folder:

{{project_root}}/social_share_templates/templates/django_social_share/templatetags/post_to_facebook.html

The reason your current configuration does not work, is that your local app has the same name as the app in site-packages. If your local app is an actual python module, you won't be able to use the app in site-packages. Otherwise, Django just looks for the templates in the app in site-packages.