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',
),
},
},
]
There are two main ways to organize your template structure in Django: the default app-level way and a custom project-level approach. By default the Django template loader will look within each app for a templates folder. But to avoid namespace issues you also need to repeat the app name in a folder below that before adding your template file.
The most common place to specify custom template tags and filters is inside a Django app. If they relate to an existing app, it makes sense to bundle them there; otherwise, they can be added to a new app.
You can either put template overrides in your project’s templates directory or in an application’s templates directory. If you have app and project templates directories that both contain overrides, the default Django template loader will try to load the template from the project-level directory first.
As a Django projects grow in size it's often more convenient to have all the templates in one place rather than hunting for them within multiple apps. With a single line change to our settings.py file, we can do this.
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.
To just change templates you don't have to fork an app. Just make sure you have set up your template loaders correctly. Usually you'd like something as: (documentation)
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'django.template.loaders.eggs.Loader',
],
This means (for your case take django_social_share/templatetags/post_to_gplus.html
as an example) - when looking for the template Django will try to find it:
in your project's template directory/directories, specified in DIRS
so if you have a file in <project_root>/templates/django_social_share/templatetags/post_to_gplus.html
it will take this one.
in your 'local' apps templates
dir, if in INSTALLED_APPS
. so you could create e.g. a social_share_custom
app, add it to INSTALLED_APPS
and create a template at:<project_root>/social_share_custom/templates/django_social_share/templatetags/post_to_gplus.html
This approach has the benefit that you could re-use the social_share_custom
easily in other projects.
in the templates
directory of your apps installed via pip (or other ways)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With