我正在尝试创建中间件,以便可选地将kwarg传递给满足条件的每个视图.
问题是我找不到如何设置中间件的示例.我见过的类覆盖了我想要的方法,process_view:
Class CheckConditionMiddleware(object):
def process_view(self, request):
return None
Run Code Online (Sandbox Code Playgroud)
但是我把这个课程放在哪里?我是否创建了一个中间件应用程序并将此类放在其中,然后在settings.middleware中引用它?
我正在创建一个自定义中间件来django编辑响应对象以充当审查员.我想找到一种方法来进行一种搜索和替换,用我选择的一个单词取代某些单词的所有实例.
我已经创建了我的中间件对象,将其添加到我MIDDLEWARE_CLASSES
的设置中并将其设置为处理响应.但到目前为止,我只找到了添加/编辑cookie,设置/删除字典项或写入html末尾的方法:
class CensorWare(object):
def process_response(self, request, response):
"""
Directly edit response object here, searching for and replacing terms
in the html.
"""
return response
Run Code Online (Sandbox Code Playgroud)
提前致谢.