2013-06-21 2 views
0

내 위시리스트에 대한 코드를 사용하고 있습니다. 내 사이트에 표시하고자하는 위시리스트의 제품이 필요하지 않습니다. 다양한 방법을 시도했지만 세션이이 작업을 수행 할 것이라고 생각합니다. 약간의 도움을주십시오.어떻게 장고보기에서 세션의 lenth을 얻을 수

어떻게 그렇게 할 수 있습니까?

@never_cache 
def wishlist(request, template="shop/wishlist.html"): 
    """ 
    Display the wishlist and handle removing items from the wishlist and 
    adding them to the cart. 
    """ 

    skus = request.wishlist 
    error = None 
    if request.method == "POST": 
     to_cart = request.POST.get("add_cart") 
     add_product_form = AddProductForm(request.POST or None, 
              to_cart=to_cart,request=request) 
     if to_cart: 
      if add_product_form.is_valid(): 
       request.cart.add_item(add_product_form.variation, 1,request) 
       recalculate_discount(request) 
       message = _("Item added to cart") 
       url = "shop_cart" 
      else: 
       error = add_product_form.errors.values()[0] 
     else: 
      message = _("Item removed from wishlist") 
      url = "shop_wishlist" 
     sku = request.POST.get("sku") 
     if sku in skus: 
      skus.remove(sku) 
     if not error: 
      info(request, message) 
      response = redirect(url) 
      set_cookie(response, "wishlist", ",".join(skus)) 
      return response 

    # Remove skus from the cookie that no longer exist. 
    published_products = Product.objects.published(for_user=request.user) 
    f = {"product__in": published_products, "sku__in": skus} 
    wishlist = ProductVariation.objects.filter(**f).select_related(depth=1) 
    wishlist = sorted(wishlist, key=lambda v: skus.index(v.sku)) 
    context = {"wishlist_items": wishlist, "error": error} 
    response = render(request, template, context) 
    if len(wishlist) < len(skus): 
     skus = [variation.sku for variation in wishlist] 
     set_cookie(response, "wishlist", ",".join(skus)) 
    return response 
+0

이 연료 소모량을 추가하는 데 사용됩니다 나는 위시리스트에있는 제품을 제거합니다. –

+0

세션 중에 제품 수가 필요합니다. –

+0

어떤 세션입니까? 쿠키를 사용하고 있는데 세션이 아닌 것 같습니다. –

답변

2

세션! = 쿠키. 세션은 백엔드의 서버에서 관리하고 쿠키는 사용자 브라우저로 전송됩니다. Django uses a single cookie to help track sessions하지만이 인스턴스에서는 단순히 쿠키를 사용하고 있습니다.

세션 프레임 워크를 사용하면 사이트 방문자별로 임의의 데이터를 저장하고 검색 할 수 있습니다. 서버 측에 데이터를 저장하고 쿠키 송수신을 추상화합니다. 쿠키에는 세션 ID가 포함되어 있습니다 (쿠키 기반 백엔드를 사용하지 않는 한) 데이터 자체는 아닙니다.

그것은 당신이 원하는 것을 이야기하기는 어렵습니다,하지만 당신은 단순히 당신이 쿠키에 저장되는 항목의 수를 얻으려면, 당신은 단순히 당신의 sku의 수를 계산하고있는 상황에 넣어해야

if len(wishlist) < len(skus): 
    skus = [variation.sku for variation in wishlist] 
    set_cookie(response, "wishlist", ",".join(skus)) 
context = {"wishlist_items": wishlist, "error": error, "wishlist_length":len(wishlist)} 
return render(request, template, context) 

사용 : 템플릿에 전송 템플릿에

{{ wishlist_length }} 

+0

고마워요 당신은 아주 많이 Logged –

관련 문제