Django - 中间件与视图/模板进行通信 [英] Django - having middleware communicate with views/templates

查看:115
本文介绍了Django - 中间件与视图/模板进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这可能是一个非常愚蠢的问题,但是我对Python / Django来说是新的,所以我不能真正把我的头围绕着它的范围概念。现在我正在撰写一个中间件类处理一些东西,我想设置我的视图和模板可以访问的全局变量。这样做的正确方式是什么?我考虑做这样的事情:



middleware.py



  from django.conf import settings 

class BeforeFilter(object):
def process_request(self,request):
settings.my_var ='Hello World'
return无



views.py



 code> from django.conf import settings 
from django.http import HttpResponse

def myview(request):
return HttpResponse(settings.my_var)

尽管这样做,我不知道是否是Django或Python方式



所以,我的问题是:

1.这是正确的方法吗?

2.如果是正确的方法是什么是正确的方法来添加可以在中间件实际模板中使用的变量?说我想评估一些东西,我想在中间件中设置一个变量 headername 作为我的网站名称,我想要能够做 {{headername}} 。按照我现在的方式进行,我必须在每个视图中的上下文中添加 headername 。有没有绕过这个?我正在思考CakePHP的 $ this-> set('headername','我的网站名称')的一些东西;

3.我我正在使用中间件类作为CakePHP的 beforeFilter 之前,在每个视图(或CakePHP中的控制器)被调用之前运行的类。这是正确的方法吗?

4.完全不相关但是一个小问题,什么是一个很好的方式打印出一个变量的内容到浏览器ala 的print_r ?说我想看到传递给视图的请求中的所有内容?/ code? $ pprint 答案?

解决方案


  1. 这不是最好的方法。您可以根据请求而不是设置设置my_var。设置是全球性的,适用于整个网站。您不想为每个请求修改它。可能会出现多次请求更新/同时读取变量的并发问题。


  2. 要访问template.my_var,您可以执行 {{request.my_var}} 。要访问您的模板中的请求变量,您必须将 django.core.context_processors.request 添加到 TEMPLATE_CONTEXT_PROCESSORS 设置。


  3. 是的。描述请求中间件的其他术语将是请求预处理器/过滤器/拦截器。


此外,如果要在模板中使用标题的常用站点名称,则可能需要检查在Django Sites应用程序中提供一个站点名称变量供您使用。


Alright, this is probably a really silly question but I am new to Python/Django so I can't really wrap my head around its scoping concepts just yet. Right now I am writing a middleware class to handle some stuff, and I want to set 'global' variables that my views and templates can access. What is the "right" way of doing this? I considered doing something like this:

middleware.py

from django.conf import settings

class BeforeFilter(object):
    def process_request(self, request):
        settings.my_var = 'Hello World'
        return None

views.py

from django.conf import settings
from django.http import HttpResponse

def myview(request):
    return HttpResponse(settings.my_var)

Although this works, I am not sure if it is the "Django way" or the "Python way" of doing this.

So, my questions are:
1. Is this the right way?
2. If it is the right way, what is the right way of adding variables that can be used in the actual template from the middleware? Say I want to evaluate something and I want to set a variable headername as 'My Site Name' in the middleware, and I want to be able to do {{ headername }} in all templates. Doing it the way I have it now I'd have to add headername to the context inside every view. Is there anyway to bypass this? I am thinking something along the lines of CakePHP's $this->set('headername','My Site Name');
3. I am using the middleware class as an equivalent of CakePHP's beforeFilter that runs before every view (or controller in CakePHP) is called. Is this the right way of doing this?
4. Completely unrelated but it is a small question, what is a nice way of printing out the contents of a variable to the browser ala print_r? Say I want to see all the stuff inside the request that is passed into the view? Is pprint the answer?

解决方案

  1. It's not the best way. You could set my_var on the request rather than on the settings. Settings are global and apply to the whole site. You don't want to modify it for every request. There could be concurrency issues with multiple request updating/reading the variable at the same time.

  2. To access request.my_var in your templates you could do {{ request.my_var }}. To get access to the request variable in your template you will have to add django.core.context_processors.request to your TEMPLATE_CONTEXT_PROCESSORS setting.

  3. Yes. Other terminology to describe request middleware would be request pre-processor/filter/interceptor.

Also, if you want to use a common Site name for the header in your templates, you might want to check out the Django Sites application which provides a site name variable for your use.

这篇关于Django - 中间件与视图/模板进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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