如何在Django中覆盖外部应用模板? [英] How to override external app template in Django?

查看:67
本文介绍了如何在Django中覆盖外部应用模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试覆盖 django-recaptcha 模板,但没有任何运气。我究竟做错了什么?
我知道在Django中覆盖外部应用程序模板,但已过时。谢谢!



django-recaptcha文件结构

  Lib / 
--site-packages /
---- captcha /
------ templates /
-------- captcha /
---------- includes /
------------ js_v2_checkbox.html

我的项目文件结构

  project / 
---- templates /
--------验证码/
------------ includes /
---------------- js_v2_checkbox.html

settings.py

 模板= [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS':[os.path.join(BASE_DIR,'templates')],
'APP_DIRS':是,
'选项':{
'context_processors':[
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib。一种uth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]


解决方案

您有两个选择:



(1)在您的设置中,如下重新排列INSTALLED_APPS:

  INSTALLED_APPS = [
.. 。
'项目',
...
'captcha',
...
]

由于模板加载程序将按照INSTALLED_APPS指定的顺序在应用程序的模板目录中查找,因此您将首先找到模板。





(2)在TEMPLATES [0] ['DIRS']中列出项目的模板文件夹,如下所示:

 模板= [
{
'BACKEND':'django.template.backends.django.DjangoTemplates',
' DIRS':[os.path.join(BASE_DIR,模板)],
APP_DIRS:是,
...
},
]

由于在APP_DIRS之前搜索了DIRS,因此将首先找到您的模板。



参考文献:



https://docs.djangoproject.com/en/3.0/howto/overriding-templates/



另一种可能的解决方案



我现在注意到 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中覆盖外部应用模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆