我正在扩展页眉和页脚,但是当我在页脚中传递数据时,它仅在主页上可见,在其他页面上不可见 [英] 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

查看:24
本文介绍了我正在扩展页眉和页脚,但是当我在页脚中传递数据时,它仅在主页上可见,在其他页面上不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

from django.shortcuts 导入渲染,重定向从 footer.models 导入页脚定义主页(请求):contact_info = Footer.objects.all()返回渲染(请求,'前端/index.html',{'contact_info':contact_info})

Index.html

<div class="gtco-widget"><h3>取得联系</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>{% 结束为 %}{% 万一 %}

在联系页面:

在索引页:

解决方案

如果您需要每个页面中的数据,那么您可以使用上下文处理器.

在您的应用程序文件夹中,创建 context_processor.py

context_processor.py 中:

def context_processor(request):上下文 = {}上下文['数据'] = '一些数据'返回上下文

settings.py 中:

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

在您的模板中.在你的情况下,你可以在你的 footer.html 中使用

{{data}}

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

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. I know that I have to pass that data on every pages but I am looking for a easy solution example:

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>

At Contact Page:

At Index Page:

解决方案

If you need your data in every pages, then you can use context processor.

In you app folder, create context_processor.py

In context_processor.py:

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

In settings.py:

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

In your templates. In your case, you can use in your footer.html

{{data}}

FYI: You don't need to work with your views also.

这篇关于我正在扩展页眉和页脚,但是当我在页脚中传递数据时,它仅在主页上可见,在其他页面上不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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