首页 > 解决方案 > Django复制block.super内容

问题描述

在此处的文档中使用模板继承,我得到了重复,如图所示:

谷歌标签引用重复

在上面的照片中,注释的 Google 跟踪代码管理器文本已被复制。显然我想把实际的标签管理器放在那里,但这是一个更简单的例子。{{ base.super }} 当我调用我使用 Oscar时,我放入子模板标签中的任何内容都会重复,所以这是我的继承方案。

奥斯卡/base.html

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

标签: djangotemplatesdjango-oscar

解决方案


看起来您只是在扩展自己的模板。看起来您正在使用如何自定义 Oscar 文档中的模板中描述的方法 1 ,其中包括简单地复制模板并修改它们。 但是你表现得好像你正在使用Method 2。我相信最简单的解决方案是只复制他们的模板而不是扩展它,或者您可以查看使用方法 2,如他们使用父目录技巧的文档中所述。或者作为方法 2 中给出的替代选项,使用django-overextends


推荐阅读