【问题标题】:Django how to reuse views functionality common to all viewsDjango如何重用所有视图共有的视图功能
【发布时间】:2017-02-11 08:05:26
【问题描述】:

我有一个 Django 模板,我们称之为 index.html,它分为 3 部分(页眉、内容和页脚)。在标题部分,我有一个搜索栏,其中包含一个下拉菜单,允许用户从中选择一个选项并根据所选选项搜索内容。我希望该标题部分包含在我未来的所有视图/模板中,并且仍然显示包含所有选项的下拉菜单。

这是我目前在我的视图文件中的内容

def index(request):
    return render(
                    request,
                    'home.html',
                    {'categories': get_all_categories()}
                )


def cart(request):
    return render(request, 'cart.html', {'categories': get_all_categories()})


def help(request):
    return render(request, 'help.html', {'categories': get_all_categories()})


def about(request):
    return render(request, 'about.html', {'categories': get_all_categories()})


def contact(request):
    return render(request, 'contact.html', {'categories': get_all_categories()})


def search(request):
    return render(request, 'search.html', {'categories': get_all_categories()})


def get_all_categories():
    return Category.objects.all()

这就是我所拥有的 cart.html {% 扩展“index.html”%}

{% block content %}

<div>
<h1> My Cart </h1>
</div>

{% endblock %}

这就是contact.html所拥有的 {% 扩展“index.html”%}

{% block content %} 

<div>
<h1> Contact </h1>
</div>

{% endblock %} 

这是 home.html 包含的内容 {% 扩展“index.html”%}

{% block content %}

<div>
<h1> Home </h1>
</div>

{% endblock %}

这现在有效,但我想知道是否有更好的方法来解决这个问题,这样我就不必在所有视图中重复相同的代码。

【问题讨论】:

    标签: django django-templates django-views


    【解决方案1】:

    您可以编写自定义context processor 以将该变量包含在您呈现的每个模板中。

    例如,编写一个如下的上下文处理器(比如context_processors.py):

    def category_context_processor(request):
        return {
            'categories': get_all_categories(),
        }
    

    并将其包含在settings.py

    TEMPLATES = [
        ...
        'OPTIONS': {
            'context_processors': [
                ...
                'myapp.context_processors.category_context_processor',
            ],
        },
    }
    

    现在变量categories 在您渲染的每个模板中都可用(无论如何,使用render 调用或RequestContext),无论您实际从视图传递的上下文如何。

    【讨论】:

      【解决方案2】:

      您也可以使用模板标签。

      polls/
          __init__.py
          models.py
          templatetags/
              __init__.py
              poll_extras.py
          views.py
      

      在你的 poll_extras.py 文件中

      from django import template
      
      register = template.Library()
      
      @register.simple_tag
      def get_categories(request, arg1, arg2, ...):
          return {
              'categories': get_all_categories(),
          }
      

      或者您可以使用带有自己模板的包含标签(在所有视图中保持相同):

      @register.inclusion_tag('categories_block_template.html')
      def get_categories(arg1, arg2, *args, **kwargs):
          categories = Category.objects.filter(field1=arg1, ...)
          ...
      

      最后,在模板中你需要加载模板标签并使用它:

      {% load poll_extras %}
      

      你可以看到更多关于模板标签here

      【讨论】:

      猜你喜欢
      • 2014-12-04
      • 2016-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-25
      • 1970-01-01
      • 2012-11-20
      • 2014-06-19
      相关资源
      最近更新 更多