从中间件传递变量到模板 [英] Pass variable from middleware to templates

查看:173
本文介绍了从中间件传递变量到模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django的首发。到目前为止,我从视图中学习了传递变量到模板。但现在我需要传递变量到我的主要布局。我可以在每个页面的def中看到它。但它的重复太多了。所以我开始了解中间件。



我创建了middlewares.py并将其包含在设置中。在middlewares.py文件中,如何将变量传递给我的主要布局?



下面是我当前的middlewares.py内容,我尝试了很多方法并对它们进行了评论,因为不工作。

  from django.db import connections 
from django.shortcuts import render,redirect

class NotificationsMiddleware(object):
def process_view(self,request,view_func,view_args,view_kwargs):

request.context_data ['notification_count'] = 2
response = view_func(request,* view_args,** view_kwargs)

返回响应

#def process_request(self,request):
#request.notification_count = 2
$ return

#def process_template_response(self,request,response):
##response.context ['notification_count'] = 2
#response.context_data ['notification_count '] = 2
##return response
#return render(request, main / locations.html')


解决方案

您可以创建一个模板上下文处理器,首先使用一个方法创建一个Python文件,该方法返回一个您需要的信息的字典,然后使用您的示例在项目设置中添加到该文件的路由:



在您的应用程序上创建一个文件 context_processors.py

  from myapp.models import MyModel 

def my_context(request):
context_data = dict()
context_data ['notification_count'] = MyModel.objects.all()。count()
return context_data

在您的 settings.py

中添加上下文处理器

  TEMPLATE_CONTEXT_PROCESSORS =(
#...
'myapp.context_proccessors.my_context',

您可以在任何模板中使用变量:

 < p> Count:{{notification_count }}< / p为H. 


I'm Django starter. So far I learned pass variable from view to template. But now I need pass variable to my main layout. I can pass it in each page's def in view. But its too much duplication. So I started learn about middleware.

I created middlewares.py and included it in settings. In middlewares.py file, how to pass variable to my main layout?

Below is my current middlewares.py content, I tried many ways and commented them out, because not working.

from django.db import connections
from django.shortcuts import render, redirect

class NotificationsMiddleware(object):
    def process_view(self, request, view_func, view_args, view_kwargs):

        request.context_data['notification_count'] = 2
        response = view_func(request, *view_args, **view_kwargs)

        return response

    # def process_request(self, request):
    #     request.notification_count = 2
    #     return

    # def process_template_response(self, request, response):
    #     # response.context['notification_count'] = 2
    #     response.context_data['notification_count'] = 2
    #     # return response
    #     return render(request, 'main/locations.html')

解决方案

You may create a template context processor, first create a python file with a method that returns a dictionary with the info you need then add the route to this file in your project settings, using your example:

create a file context_processors.py on your app:

 from myapp.models import MyModel

 def my_context(request):
    context_data = dict()
    context_data['notification_count'] = MyModel.objects.all().count()
    return context_data

The add the context processor in your settings.py

TEMPLATE_CONTEXT_PROCESSORS = (
    #  ...
    'myapp.context_proccessors.my_context',
)

The you can use the 'variable' in any template:

<p>Count: {{ notification_count }}</p>

这篇关于从中间件传递变量到模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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