gpt4 book ai didi

python - 在第三方 Django 应用程序中自定义模板

转载 作者:太空宇宙 更新时间:2023-11-03 12:56:52 25 4
gpt4 key购买 nike

我在我的 Django 项目中使用第三方应用程序 (django-social-share),但我需要自定义模板。我不确定该怎么做——我尝试的一切都使用默认模板。

当前默认模板存储在:

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

我已经定制了一个:

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

我的模板设置:

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',
),
},
},
]

最佳答案

您可以将模板放在项目中的单独应用中。 Django 按照在 INSTALLED_APPS 中定义的顺序在您的应用中查找模板,并使用第一个匹配项:

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)

因此创建一个应用程序,例如social_share_templates,并将其放在INSTALLED_APPS 之前 django_social_share。然后将模板添加到此文件夹中:

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

您当前配置不起作用的原因是您的本地应用与站点包中的应用同名。如果您的本地应用程序是一个实际的 python 模块,您将无法在站点包中使用该应用程序。否则,Django 只会在站点包中的应用程序中查找模板。

关于python - 在第三方 Django 应用程序中自定义模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38341254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com