【问题标题】:How to get something to appear on every page in Django?如何让某些东西出现在 Django 的每个页面上?
【发布时间】:2010-01-04 01:38:00
【问题描述】:

我很好奇处理让某些内容出现在每个页面或多个页面上的最佳实践方法,而不必像这样手动将数据分配给每个页面:

# views.py

def page0(request):
    return render_to_response(
        "core/index.html",
        {
            "locality": getCityForm(request.user),
        },
        context_instance=RequestContext(request)
    )

def page1(request):
    return render_to_response(
        "core/index.html",
        {
            "locality": getCityForm(request.user),
        },
        context_instance=RequestContext(request)
    )
...
def page9(request):
    return render_to_response(
        "core/index.html",
        {
            "locality": getCityForm(request.user),
        },
        context_instance=RequestContext(request)
    )

现在我可以想出几种方法来做到这一点,包括编写我自己的上下文或一些中间件,当然,在每一页上复制/粘贴这个 locality 分配......我只是不确定最好的方法来做到这一点。不过我很确定这不是最后一个。

【问题讨论】:

    标签: python django


    【解决方案1】:

    你想要一个context processor。它们生成的数据包含在创建为RequestContext 的每个上下文中。它们非常适合这个。

    结合显示常见事物的基本模板,您可以摆脱大量复制和粘贴代码的需要。

    【讨论】:

      【解决方案2】:

      在模板引擎中使用继承:

      有一个包含通用代码的 base.html:

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
      <head>
      <link rel="stylesheet" href="style.css" />
      <title>{% block title %}My amazing site{% endblock %}</title>
      </head>
      
      <body>
      <div id="sidebar">
          {% block sidebar %}
          <ul>
              <li><a href="/">Home</a></li>
              <li><a href="/blog/">Blog</a></li>
          </ul>
          {% endblock %}
      </div>
      
      <div id="content">
          {% block content %}{% endblock %}
      </div>
      </body>
      </html>
      

      然后在需要该通用代码的每个页面中,只需:

      {% extends "base.html" %}
      {% block title %}My amazing blog{% endblock %}
      {% block content %}
      {% for entry in blog_entries %}
      <h2>{{ entry.title }}</h2>
      <p>{{ entry.body }}</p>
      {% endfor %}
      {% endblock %}
      

      http://docs.djangoproject.com/en/dev/topics/templates/#id1

      这与上下文处理相结合将消除大量重复代码。

      【讨论】:

      • 您没有解决他在问题中显示的问题:查看在许多视图中重复的代码。
      • 其实两者都有!它是地板蜡甜点浇头!我们使用上下文处理器和多级模板继承方案的组合。上下文处理器为我们提供所有视图的数据,模板继承意味着数据位于页面上的正确位置。
      【解决方案3】:

      中间件是一种选择,或者您可以编写自定义template tag

      【讨论】:

        【解决方案4】:

        要完全按照您的要求做,我只需定义一个函数:

        def foobar(req):
            return render_to_response(
                "core/index.html",
                {
                    "locality": getCityForm(req.user),
                },
                context_instance=RequestContext(req)
            )
        

        把它放在某个模块myfunreturn myfun.foobar(request) 任何需要的地方。您可能需要更多参数,但只要保持简单,定义中间件、使用 OOP 等就更简单了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多