Bootstrap

《Django 5 By Example》阅读笔记:p339-p358

《Django 5 By Example》学习第13天,p359-p382总结,总计24页。

一、技术总结

1.session

(1)session 存储方式

  • Database sessions

  • File-based sessions

  • Cached sessions

  • Cached database sessions

  • Cookie-based sessions

(2)设置 CART_SESSION_ID

# settings.py
CART_SESSION_ID = 'cart'

(3)使用示例

from django.conf import settings
from decimal import Decimal
from shop.models import Product


class Cart:
    def __init__(self, request):
        """
        初始化 cart
        """
        self.session = request.session
        # todo 这里的 cart 的类型是:
        cart = self.session.get(settings.CART_SESSION_ID)
        if not cart:
            cart = self.session[settings.CART_SESSION_ID] = {}
        self.cart = cart

    def save(self):
        """

        """
        self.session.modified = True

    def add(self, product, quantity=1, override_quantity=False):
        """
        添加商品到购物车
        """
        product_id = str(product.id)
        if product_id not in self.cart:
            self.cart[product_id] = {
                'quantity': 0,
                'price': str(product.price)
            }
        if override_quantity:
            self.cart[product_id]['quantity'] = quantity
        else:
            self.cart[product_id]['quantity'] += quantity

        self.save()

    def remove(self, product):
        """
        删除购物车中的商品
        """
        product_id = str(product.id)
        if product_id in self.cart:
            del self.cart[product_id]
            self.save()

    def get_total_price(self):
        """
        统计购物车里商品价格总和
        """
        return sum(Decimal(item['price']) * item['quantity'] for item in self.cart.values())

    def clear(self):
        """
        清空购物车 session
        """
        del self.session[settings.CART_SESSION_ID]
        self.save()

    def __iter__(self):
        product_ids = self.cart.keys()
        products = Product.objects.filter(id__in=product_ids)
        cart = self.cart.copy()
        for product in products:
            cart[str(product.id)]['product'] = product
        for item in cart.values():
            item['price'] = Decimal(item['price'])
            item['total_price'] = item['price'] * item['quantity']
            yield item

    def __len__(self):
        """
        统计购物车商品数量总和
        """
        return sum(item['quantity'] for item in self.cart.values())

2.Context processors

(1)什么是 context processors?

p370, A context processor is a Python function that takes the request object as an argument and returns a dictionary that gets added to the request context.

二、英语总结(生词:2)

1.coerce

(1)coerce: co-(“together”) + arecere(“to restrain”)

vt. to persuade sb forcefully to do sth(强制)。

2.get added to

p370, A context processor is a Python function that takes the request object as an argument and returns a dictionary that gets added to the request context.

虽然表被动意思,但是用的是 get added to, 类似于 be added to。

三、其它

chapter 08 今日简评:技术上主要涉及 session , context processor 的使用,除此之外更多的是业务处理。

四、参考资料

1. 编程

(1) Antonio Melé,《Django 5 By Example》:https://book.douban.com/subject/37007362/

2. 英语

(1) Etymology Dictionary:https://www.etymonline.com

(2) Cambridge Dictionary:https://dictionary.cambridge.org

在这里插入图片描述

欢迎搜索及关注:编程人(a_codists)

;