def latest_entry(request, *args, **kwargs):
from urllib.parse import urlparse
# print('latest_entry >>>', request, args, kwargs)
print(request.get_full_path_info())
try:
p = urlparse(request.get_full_path_info())
project_id = None
for rec in p.query.split('&'):
items = str(rec).split('=')
if items[0] == 'project':
project_id = items[1]
break
print('find project id >>>', project_id)
pro = m.Project.objects.get(id=project_id)
if pro:
print('pro.date_updated >>>',pro.date_updated)
return pro.date_updated
return datetime.now()
except:
print('latest_entry got exception!!!')
traceback.print_exc()
return datetime.now()
# 由于list函数第一个参数是self,所以要再包一层。list函数是mixins.ListModelMixin的方法
@method_decorator(condition(last_modified_func=latest_entry) )
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
参考:
https://docs.djangoproject.com/zh-hans/4.1/topics/conditional-view-processing/