从中间件传递变量到模板

编程入门 行业动态 更新时间:2024-10-09 08:33:41
本文介绍了从中间件传递变量到模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我是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>

更多推荐

从中间件传递变量到模板

本文发布于:2023-11-16 11:00:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603677.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   中间件   模板

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!