Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用django在基本模板中创建动态菜单_Django_Django Templates_Renderpartial - Fatal编程技术网

如何使用django在基本模板中创建动态菜单

如何使用django在基本模板中创建动态菜单,django,django-templates,renderpartial,Django,Django Templates,Renderpartial,我即将开始一个新的项目,我认为这次django是一个不错的选择。在过去的两周里,我一直在阅读这些文档,看起来很有希望 好的,问题是我找不到任何关于(在C#MVC中称为)部分渲染的东西。例如,如果我想要一个动态菜单,其中菜单项来自数据库,那么我希望基础模板(或母版页)在每个请求上呈现菜单(部分呈现器调用另一个操作或呈现带有一些会话数据的模板)。因此,菜单是免费的,只要我的模板继承了这个基础模板 老实说,我不知道如何做到这一点 我想要的是基本模板中的一些代码,它使用子模板中不包含的数据。我不想每次

我即将开始一个新的项目,我认为这次django是一个不错的选择。在过去的两周里,我一直在阅读这些文档,看起来很有希望

好的,问题是我找不到任何关于(在C#MVC中称为)部分渲染的东西。例如,如果我想要一个动态菜单,其中菜单项来自数据库,那么我希望基础模板(或母版页)在每个请求上呈现菜单(部分呈现器调用另一个操作或呈现带有一些会话数据的模板)。因此,菜单是免费的,只要我的模板继承了这个基础模板

老实说,我不知道如何做到这一点


我想要的是基本模板中的一些代码,它使用子模板中不包含的数据。我不想每次调用render\u to\u response('child\u content.html',context)时都包含一个额外的变量(可能是'menu\u list\u items')。这可能吗

谢谢

您可以使用或来提供此功能

上下文处理器是一个简单的函数,它可以向每个RequestContext添加对象。自定义模板标记可以有自己的模板片段和上下文,可以为您呈现菜单。

您可以使用或提供此功能


上下文处理器是一个简单的函数,它可以向每个RequestContext添加对象。自定义模板标记可以有自己的模板片段和上下文,可以为您呈现菜单。

对于模板重用:您应该为通用布局创建一个基础模板,并为各个页面使用详细模板。本手册已经详细介绍了这一点

我倾向于为这些通用部分(例如,一个突出显示使用站点当前部分的菜单)创建我自己的
render\u to\u response
函数,类似于以下内容:

from django.shortcuts import render_to_response as django_render_to_response
def render_to_response(template, params, context_instance):
    return django_render_to_response(template,
                        AppendStandardParams(request, params),
                        context_instance)
然后,
ApplyStandardParams
方法根据当前路径配置菜单:

def AppendStandardParams(request, params):
    if request.META['PATH_INFO'].startswith('/customer'):
        params['category'] = 'customer'
        params['title'] = 'Customer overview'
    # and so on for all different parts
本例中的
category
title
标记是一些用于突出显示菜单、配置标题等的值。例如:

<!-- Customer menu entry: change class if this is the current category. -->
<li{% if category == "customer" %} class="selected"{% endif %}>Customers</li>

顾客

最后,为了在视图中使用它,我只需执行类似于从lib.views import*的操作,而不是常规的
render\u to\u响应
导入,这使我的自定义版本在视图中可用。这样,视图中所有代码的语法都保持不变,但我不必在每次创建新视图或应用程序时自定义菜单。

对于模板重用:您应该为通用布局创建一个基本模板,并为各个页面使用详细模板。本手册已经详细介绍了这一点

我倾向于为这些通用部分(例如,一个突出显示使用站点当前部分的菜单)创建我自己的
render\u to\u response
函数,类似于以下内容:

from django.shortcuts import render_to_response as django_render_to_response
def render_to_response(template, params, context_instance):
    return django_render_to_response(template,
                        AppendStandardParams(request, params),
                        context_instance)
然后,
ApplyStandardParams
方法根据当前路径配置菜单:

def AppendStandardParams(request, params):
    if request.META['PATH_INFO'].startswith('/customer'):
        params['category'] = 'customer'
        params['title'] = 'Customer overview'
    # and so on for all different parts
本例中的
category
title
标记是一些用于突出显示菜单、配置标题等的值。例如:

<!-- Customer menu entry: change class if this is the current category. -->
<li{% if category == "customer" %} class="selected"{% endif %}>Customers</li>

顾客
最后,为了在视图中使用它,我只需执行类似于从lib.views import*的操作,而不是常规的
render\u to\u响应
导入,这使我的自定义版本在视图中可用。这样,视图中所有代码的语法都保持不变,但我不必每次创建新视图或应用程序时都自定义菜单