etag&last_modified&condition& django.middleware.http.ConditionalGetMiddleware

Posted 本站大佬

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了etag&last_modified&condition& django.middleware.http.ConditionalGetMiddleware相关的知识,希望对你有一定的参考价值。

views.py

from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponseNotFound
from django.shortcuts import render

# Create your views here.
from django.views.decorators.http import etag, last_modified, condition

from syboy1.models import symodel


def etag_funcview(request):
    etag_head = hash(str(symodel.objects.latest('pubdate').name))
    print(etag_head)
    return str(etag_head)


def last_modifiedview(request):
    return symodel.objects.latest('pubdate').pubdate

# @condition(etag_func=etag_funcview,last_modified_func=last_modifiedview)
# @last_modified(last_modifiedview)
# @etag(etag_funcview)
def aa1(request):
    name = symodel.objects.latest('pubdate').name
    print(name)
    return HttpResponse(name)

















def aa(request):
    return HttpResponseNotFound("None")


def pa(request):
    return HttpResponse('pa')

setting.py

MIDDLEWARE = [

    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.contrib.redirects.middleware.RedirectFallbackMiddleware',
'django.middleware.http.ConditionalGetMiddleware',

]
USE_ETAGS=True

以上是关于etag&last_modified&condition& django.middleware.http.ConditionalGetMiddleware的主要内容,如果未能解决你的问题,请参考以下文章

如何在 oracle 中使用空值检查 last_modified 日期

Web缓存杂谈--Etag & If-None-Match

我可以依靠 ETags 进行缓存控制吗

0607am抽象类&接口&析构方法&tostring&小知识点

ETag

如果 ETag 不匹配,如何使用 ETag 在插入时抛出异常(除非它是 *)