【问题标题】:Fetch Data from DB in base template Django Python在基本模板 Django Python 中从数据库中获取数据
【发布时间】:2014-05-17 07:35:48
【问题描述】:

我正在使用 Python Django 开发一个应用程序(简单的在线商店)我在模板中创建了一个 base.html 文件,然后所有其他 html 文件都从该文件扩展,但问题是,我需要 base.html 文件可以访问数据库,这样我就可以在文件的标题中显示产品类别的所有名称

 <!DOCTYPE html>
 <html lang="en">
 <head>
      <link rel="stylesheet" href="style.css" />
     <title>{% block title %}My amazing site{% endblock %}</title>
{% load staticfiles %}
 <link rel="stylesheet" type="text/css" media="screen" href="{% static "css/main.css"  %}"/>            
  <link rel="stylesheet" type="text/css" media="screen" href="{% static "bootstrap/css/bootstrap.min-rtl.css" %}" >
  <link rel="stylesheet" type="text/css" media="screen" href="{% static   "bootstrap/css/bootstrap-responsive.min-rtl.css" %}" >
  </head>

 <body class="container-fluid" >
<div class="row-fluid" > 
<div id="sidebar" class="span12">
    {% block sidebar %}

    {% endblock %}
</div>
</div>
<div class="row-fluid">
    <div class="span8 offset2">
        {% block slider %}

        {% endblock %}
    </div>      
</div>
  </body>
  </html>

我应该如何创建一些可以通过整个应用程序访问的变量,并且我不必为每个页面分别从数据库中获取它们?

提前感谢您的帮助

【问题讨论】:

    标签: django django-templates django-views


    【解决方案1】:

    您可以编写模板上下文处理器或模板标签。

    模板上下文处理器

    settings.py

    from django.conf import global_settings
    TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
        "myapp.processor.foos",
    )
    

    myapp/processor.py

    from myproject.myapp.models import Foo
    
    def foos(request):
        return {'foos': Foo.objects.all()}
    

    然后你可以在任何模板中使用 {{ foos }}

    如果你在你的 render_to_response 中传递 context_instance

    return render_to_response('index.html', {}, context_instance=RequestContext(request))
    

    模板标签

    【讨论】:

    • 感谢您的回答,但在查看时我的代码是这样的 t = loader.get_template('home.html'); c = Context({ 'first' : first_list, 'second' : second_list });返回 HttpResponse(t.render(c));我应该如何修改它以使其与处理器变量一起使用?
    【解决方案2】:

    您想使用template context processors。我在 Django 文档中找不到非常清晰的描述,但是模板上下文处理器基本上是通过请求调用的可调用对象,并返回一个上下文变量字典以供所有模板使用(严格来说不是 all 模板,但如果你使用 django.shortcuts.render,你就会被覆盖)。

    定义您的活动模板上下文处理器有点尴尬,因为您不能只是将您的处理器添加到默认处理器列表中,而且您通常也希望保留默认处理器,因此您需要在设置文件中转储默认处理器的整个列表以及您的处理器:

    在您的设置中:

    TEMPLATE_CONTEXT_PROCESSORS = (
    
        # the default processors:
        "django.contrib.auth.context_processors.auth",
        "django.core.context_processors.debug",
        "django.core.context_processors.i18n",
        "django.core.context_processors.media",
        "django.core.context_processors.static",
        "django.core.context_processors.tz",
        "django.contrib.messages.context_processors.messages",
    
        # your processor that will populate the context with a list of categories:
        "myapp.context_processors.categories",
    
    )
    

    在你的myapp/context_processors.py:

    def categories(request):
        return {
            'categories': Category.objects.all()
        }
    

    根据需要修改文件名和类别检索。

    【讨论】:

    • 感谢您的回答,但在查看时我的代码是这样的 t = loader.get_template('home.html'); c = Context({ 'first' : first_list, 'second' : second_list });返回 HttpResponse(t.render(c));渲染视图时如何将全局变量传递给模板
    • 您需要使用RequestContext(request, {...}) 而不是普通的Context。常规 Contexts 不使用模板上下文处理器,但 RequestContexts 使用。
    猜你喜欢
    • 2023-04-09
    • 1970-01-01
    • 2016-05-24
    • 2022-01-10
    • 2011-04-23
    • 1970-01-01
    • 2016-06-28
    • 2016-09-08
    • 2015-03-04
    相关资源
    最近更新 更多