django - Django复制block.super内容
问题描述
在此处的文档中使用模板继承,我得到了重复,如图所示:
在上面的照片中,注释的 Google 跟踪代码管理器文本已被复制。显然我想把实际的标签管理器放在那里,但这是一个更简单的例子。{{ base.super }}
当我调用我使用 Oscar时,我放入子模板标签中的任何内容都会重复,所以这是我的继承方案。
#from oscar module
#relevent sections only
{% load i18n %}
{% load static %}
<!DOCTYPE html>
<html lang="{{ LANGUAGE_CODE|default:"en-gb" }}" class="{% block html_class %}no-js{% endblock %}">
<head>
{% block tracking %}
{# Default to using Google analytics #}
{% include "oscar/partials/google_analytics.html" %}
{% endblock %}
</head>
我的基本覆盖
# located in my procject at 'oscar/base.html'
{% extends 'oscar/base.html' %}
{% block tracking %}
{{ block.super }}
<!-- Google Tag Manager -->
<!-- End Google Tag Manager -->
{% endblock %}
以下是我认为可能涉及的其他内容。欢迎任何有助于我调试的故障排除提示。
我的观点
from django.http import HttpResponse
from django.views.generic.base import TemplateView
class BaseTestView(TemplateView):
template_name = "oscar/base.html"
网址.py
from myurls import BaseTestView
urlpatterns = [
path('i18n/', include('django.conf.urls.i18n')),
path('admin/', admin.site.urls),
path('base_test', BaseTestView.as_view(), name='base+test'),
##OSCAR###############################################################
path('', include(apps.get_app_config('oscar').urls[0])),
##WAGTAIL#############################################################
path('cms/', include(wagtailadmin_urls)),
path('documents/', include(wagtaildocs_urls)),
path('pages/', include(wagtail_urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) \
+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.py 模板
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
location('templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
####DEFAULT################################################
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
####DEFAULT################################################
# 'uniquesite.context_processors.metadata',
####OSCAR################################################
'oscar.apps.search.context_processors.search_form',
'oscar.apps.checkout.context_processors.checkout',
'oscar.apps.communication.notifications.context_processors.notifications',
'oscar.core.context_processors.metadata',
],
},
},
]
解决方案
看起来您只是在扩展自己的模板。看起来您正在使用如何自定义 Oscar 文档中的模板中描述的方法 1 ,其中包括简单地复制模板并修改它们。
但是你表现得好像你正在使用Method 2。我相信最简单的解决方案是只复制他们的模板而不是扩展它,或者您可以查看使用方法 2,如他们使用父目录技巧的文档中所述。或者作为方法 2 中给出的替代选项,使用django-overextends。
推荐阅读
- php - 卷曲 OpenSSL 错误 141A318A tls_process_ske_dhe:dh 密钥太小
- react-native - 什么时候需要编写 Redux Persist Migration
- ldap - Atlassian(Jira、Confluence)LDAP 配置
- python - SyntaxError:使用带有 Selenium 和 Python 的 xpath 时出现无效语法错误
- amazon-web-services - 使用 S3DistCp 覆盖 S3 中的现有文件
- python - 为什么 combine 函数被调用了 3 次?
- react-native - 使 Tab.Navigator 在登录页面消失
- r - r 将点和点标签添加到水平图
- ios - 无法将数据从 Firebase 附加到数组 SwiftUI
- flutter - 每个项目的计数器