问题描述
我尝试覆盖 django-recaptcha
模板,但没有任何运气。我究竟做错了什么?
我知道
另一种可能的解决方案
我现在注意到 captcha / includes / js_v2_checkbox.html
被 captcha / widget_v2_checkbox.html
。
我不确定 widget_v2_checkbox到底会发生什么.html
是从验证码模块中加载的...因此,我会将包括 widget_v2_checkbox.html
复制到项目的模板文件夹中。 / p>
为了保持一致性,您可能还决定将完整的 templates / captcha文件夹内容复制到项目中。
只要注意这些模板将来可能发生的变化升级验证码模块。
I tried overriding a django-recaptcha
template without any luck. What am I doing wrong?I am aware of Override templates of external app in Django, but it is outdated. Thanks!
django-recaptcha file structure
Lib/
--site-packages/
----captcha/
------templates/
--------captcha/
----------includes/
------------js_v2_checkbox.html
my project file structure
project/
----templates/
--------captcha/
------------includes/
----------------js_v2_checkbox.html
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'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',
],
},
},
]
You have two options:
(1) In your settings, rearrange INSTALLED_APPS as follows:
INSTALLED_APPS = [
...
'project',
...
'captcha',
...
]
since the template loader will look in the app’s templates directory following the order specified by INSTALLED_APPS, you're template will be found first.
or
(2) List project's templates folder in TEMPLATES[0]['DIRS'] as follows:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
...
},
]
Since, DIRS is searched before APP_DIRS, you're template will be found first.
References:
https://docs.djangoproject.com/en/3.0/howto/overriding-templates/
Another possible solution
I notice now that captcha/includes/js_v2_checkbox.html
is included by captcha/widget_v2_checkbox.html
.
I'm not sure about what happens exactly when widget_v2_checkbox.html
is loaded from captcha module ... so I would duplicate the "including" widget_v2_checkbox.html
as well into your project's templates folder.
You might also decide to copy the full "templates/captcha" folder contents into you project, for consistency.
Just keep an eye on possibile future changes of those templates when upgrading the captcha module.
这篇关于如何在Django中覆盖外部应用模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!