3

私は Django に関連するすべての初心者に過ぎないので、混乱を招く説明を前もってお詫びします。eコマースモジュールが添付されたDjangoベースのWebサイトで、いくつかのページ読み込みの問題を解決しようとしています。

最初のページ読み込み時間は約 5 秒と非常に長く、その後はすべてがスムーズに進みます。

この膨大な読み込み時間は、製品に関連するすべてのページ (私の場合は単一の製品ページとカート) で発生しています。奇妙なことに、リストは問題ありません(カテゴリページ)。

次の推測では、読み込み時間は製品関連のクエリが原因であると考えられるため、コードをさらに検索したところ、「product_view」定義を含む main.py ファイルにたどり着きました。

コードは次のようになります。

def product_view(request,shortcode,product_id,variation_id,stub):
    product = get_object_or_404(Product.unmoderated.select_related(),pk=product_id)
    variation = get_object_or_404(ProductVariation.objects.select_related(),pk=variation_id)
    print variation.__dict__
    if not product.active:
        c = {}
        return render_to_response('main/product_inactive.html', c, context_instance=RequestContext(request))


    links = ['main_product_view','main_product_variation'+str(variation.id)]

    c = {'links':links, 'product':product, 'variation':variation, 'categories':get_categories(), 'brands':get_brands(), 'title':product.name}
    c.update(csrf(request))

    return render_to_response('main/product.html', c,
                          context_instance=RequestContext(request))


def product_view_json(request,shortcode,product_id,variation_id,stub):
    try:
        product = get_object_or_404(Product,pk=product_id)
        variation = get_object_or_404(ProductVariation,pk=variation_id)

        thumbnail = get_thumbnail(variation.image, '220x220', crop='center', quality=80)

        d = {"title":product.name, "brand":product.merchant.name, "price":str(product.base_price+variation.price), "image":thumbnail.url}

        out = json.dumps(d)

        r = HttpResponse(out)
        r['Access-Control-Allow-Origin'] = '*';

        return r
    except:
        return HttpResponse("Error")

この時点で、私は道に迷いました。この問題を解決するための正しい道を進んでいるかどうか、または他に何をすべきかわかりません。

また、欠落している画像がいくつかありますが、まだそれらを打ち負かしていませんが、現時点では完全に関連しているわけではありません. さらに、memcache が有効になり、静的テンプレート ファイルはページの読み込み時間にまったく影響を与えず (テンプレートはテキストのみに取り除かれます)、Apache 環境で mod_pagespeed モジュールが有効になります。

これに関するご意見をいただければ幸いです。ありがとうございました!

4

2 に答える 2

0

product_viewこれらの行は正しくないと思います

product = get_object_or_404(Product.unmoderated.select_related(),pk=product_id)
variation = get_object_or_404(ProductVariation.objects.select_related(),pk=variation_id)

に変更します

product = get_object_or_404(Product,pk=product_id)
variation = get_object_or_404(ProductVariation,pk=variation_id)

.select_related()、すべてのオブジェクトとその関連オブジェクトをプリフェッチしてロードしようとしますが、これはリソースを消費します。

于 2013-06-13T09:24:18.590 に答える
0

django の問題ではなく、HTML の問題のようです。いくつかのことを調べてください

  • ポップアップを使用している場合、ページの読み込み時にポップアップが読み込まれるため、読み込み時間が長くなります。

  • 次に、.js ファイルと .css ファイルをリモートで呼び出しているか、サーバーに配置されているかを確認します。したがって、それらがローカルである場合、応答が向上します。

これらを見て、存在するものがあれば教えてください

于 2013-06-13T11:20:05.453 に答える