我正在扩展我的页眉和页脚,但是当我在页脚中传递数据时,它只在主页上可见,在其他页面上不可见

I am extending my header and footer but when I pass data in footer it is visible only at home page not on other pages

我正在扩展我的页眉和页脚,但是当我在页脚中传递数据时,它只在主页上可见,在其他页面上不可见。 我知道我必须在每个页面上传递该数据,但我正在寻找一个简单的解决方案 示例:

from django.shortcuts import render,redirect
from footer.models import Footer

   def home(request):
       contact_info = Footer.objects.all()
       return render(request,'frontend/index.html', {'contact_info':contact_info})

Index.html

<div class="col-md-3 col-md-push-1">
                <div class="gtco-widget">
                    <h3>Get In Touch</h3>
                    <ul class="gtco-quick-contact">

                        {% if contact_info %}
                        {% for i in contact_info %}
                        <li><a href="tel:{{ i.phone }}"><i class="icon-phone"></i>{{ i.phone}}</a></li>

                        
                        <li><a href="mailto:{{ i.email }}"><i class="icon-mail2"></i>{{ i.email }}</a></li>


                        <li><a href="http://maps.google.com/?q={{ i.location }}"><i class="ti-location-pin"></i>{{ i.location }}</a></li>
                        {% endfor %}

                        {% endif %}

                    </ul>
                </div>
            </div>

在联系页面:

在索引页:

如果每个页面都需要数据,则可以使用上下文处理器。

在你的应用文件夹中,创建context_processor.py

context_processor.py:

def context_processor(request):
    context = {}
    context['data'] = 'Some data'
    return context    

settings.py中:

TEMPLATES = [
    {
        ...,
        'OPTIONS': {
            ...,
            'app_name.context_processor.context_processor'
        }
    }
]

在您的模板中。在您的情况下,您可以在 footer.html

中使用
{{data}}

仅供参考:您也不需要处理您的视图。

context_processor.py

from designzoned.models import ServicesModel
def context_processor(request):
   context = {}
   context['service_list'] = ServicesModel.objects.all()
   return context

settings.py

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
            'django.template.context_processors.request',
            # In below line you have to write like this
            'designzoned.context_processor.context_processor',
        ],
    },
},

]

header.html

<ul class="dropdown-menu">
   {% for service in service_list %}
      <li><a href="{% url 'services_detail' service.slug %}">{{ 
       service.name }}</a></li>
   {% endfor %}
</ul>