from django.utils.deprecation import MiddlewareMixin
class MultipleProxyMiddleware(MiddlewareMixin):
FORWARDED_FOR_FIELDS = [
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED_HOST',
'HTTP_X_FORWARDED_SERVER',
]
def process_request(self, request):
"""
Rewrites the proxy headers so that only the most
recent proxy is used.
"""
for field in self.FORWARDED_FOR_FIELDS:
if field in request.META:
if ',' in request.META[field]:
parts = request.META[field].split(',')
request.META[field] = parts[-1].strip()
我的理解是你想根据不同的请求类型渲染不同的模板么?可以复现 get_template_names 方法:
这个方法来自
TemplateResponseMixin,用于获取 django 视图渲染的模板,建议看一下 ListView 的源代码就清楚了。试试这样的,不是用self,你类中应该写一个方法,这个方法其中有一个参数 是request
from django.utils.deprecation import MiddlewareMixin
class MultipleProxyMiddleware(MiddlewareMixin):
django的CBV本身是有get()方法的,get(self, request, args, *kwargs),这里面是有你想要的request参数的,其实首先要搞清楚你的Class View的调用顺序。给你提供一个有关django CBV的连接,我想里面会帮助到你。
https://ccbv.co.uk/