django 中使得緩存方法

? ? 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函數(shù)第一個(gè)參數(shù)是self,所以要再包一層。list函數(shù)是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/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容