0

私のdjangoデータベースにはモード名Photoがあります。ビューにはメソッド get_photos があり、すべての写真を一覧表示します。テーブルに写真を追加するためのupload_photoがあります。

問題は言うことです。

  1. 今、私は 5 枚の写真を持っています。get_photos を呼び出すと、5 枚の写真を含むリストが返されます。
  2. 私は写真と成功をアップロードします
  3. get_photos を呼び出すと、5 枚の写真が返されることもあれば、6 枚の写真が返されることもあります。
  4. 私はdjangoサーバーを再起動します。私はいつも6枚の写真を手に入れます。

どうすれば問題を解決できますか。ありがとう 。

以下は get_all_photos のビューメソッドです

@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")
4

1 に答える 1

0

ここでいくつかのことができると思います。まず、@never_cache デコレーターを get_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_cache ソリューションで十分かもしれませんが、ヒントとして上記を含めたいと思いました:)

于 2013-10-10T08:30:04.583 に答える