Python django缓存未更新
我的django数据库有一个模式名Photo。视图有一个获取照片的方法,该方法将列出所有照片。并上传一张照片,将照片添加到表中 问题是,他说Python django缓存未更新,python,django,caching,Python,Django,Caching,我的django数据库有一个模式名Photo。视图有一个获取照片的方法,该方法将列出所有照片。并上传一张照片,将照片添加到表中 问题是,他说 现在我有5张照片,我打电话给get_photos会返回一个包含5张照片的列表 我上传了一张照片并成功了 我打电话给get_photos,我会在某个时间返回5张照片,有时返回6张照片 我重新启动django服务器。我总是会得到6张照片 我怎样才能解决这个问题。谢谢 下面是获取所有照片的查看方法 @csrf_exempt def photos(request)
@csrf_exempt
def photos(request):
if request.method == 'POST':
start_index = request.POST['start_index']
else:
start_index = request.GET['start_index']
start_index=int(start_index.strip())
photos_count = Photo.objects.all().count()
allphotos = Photo.objects.all().order_by('-publish_time')[start_index: start_index+photo_page_step]
retJson = {}
retJson["code"]=200 #ok
data = {}
data["count"]=photos_count
photos = []
for p in allphotos:
photo = json_entity.from_photo(p,True);
photos.append(photo)
data["photos"]=photos
retJson["data"]=data
return HttpResponse(simplejson.dumps(retJson), mimetype="application/json")
我想你可以在这里做几件事。首先,您可以将@never\u缓存装饰器添加到get\u photos视图中:
from django.views.decorators.cache import never_cache
@never_cache
def get_photos(request):
...
这将永远不会缓存适合您的情况的页面。或者,您可以缓存照片,然后在上载新照片时,使缓存过期:
from django.core.cache import cache
def get_photos(request):
photos = cache.get('my_cache_key')
if not photos:
# get photos here
cache.set('my_cache_key', photos)
....
def upload_photo(request):
# save photo logic here
cache.set('my_cache_key', None) # this will reset the cache
也许never_缓存解决方案就足够了,但我想把上面的内容作为一个提示:)请发布一些代码。至少查看和获取照片的代码。如果没有更多的细节,没有人能够帮助你。我认为这不是视图方法的问题,我认为我应该进行一些django配置,但我不知道。为什么你认为这是缓存问题?你配置缓存了吗?没有,我没有配置。你在Photo类上有自定义管理器吗?