第二 Session 没有到期日期的引擎

Second Session ENGINE without expiry date

我有 Product 型号:


class Product(models.Model):
    title = models.CharField(max_length = 25)
    description = models.TextField(blank = True, null = True)
    price = models.IntegerField(default = 0)

    image = models.ImageField(upload_to = 'products/images', null = True, blank = True)
    image_url = models.URLField(null = True, blank = True)

    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)

    product_views = models.IntegerField(default = 0)

    def __str__(self):
        return self.title

    class Meta:
        ordering = ['title']

在每个用户视图中,我将一个添加到 product_viewsviews.py:

def product_detail_view(request, id):
    if 'views' not in request.session:
        request.session['views'] = []
    arr = request.session['views']

    # obj = Product.objects.get(id = id)
    obj = get_object_or_404(Product, id = id)
    if id not in arr:
        arr.append(id)
        request.session['views'] = arr
        obj.product_views = obj.product_views + 1
        obj.save()

    context = {
        'object': obj,
    }
    return render(request, 'home/detail.html', context)

我在settings.py中设置了SESSION_COOKIE_AGE = 86400。它很好用。但是有一个问题。在我的应用程序中,用户可以登录,一天后,他必须再次登录。我想在我的登录中添加 Remember me 复选框,但这是个问题。我无法为特定密钥设置特定的过期时间。所以我必须制作自己的第二个 Session 引擎。我写了这个:

class ProductViews(models.Model):
    product = models.ForeignKey(Product, on_delete = models.CASCADE, related_name = 'views', null = True, blank = True)
    viewer = models.CharField(max_length = 36, null=True, blank=True) # Here, I can take IP if viewer isn't logged in
    user = models.ForeignKey(User, on_delete = models.CASCADE, related_name = 'viewed_products', null = True, blank = True)
    datetime = models.DateTimeField(default = django.utils.timezone.now)

我可以通过这个检查到期时间:

    dateta = product_v.datetime

    time_now = datetime.datetime.now(dateta.tzinfo)  # current time
    time_now = time_now - timedelta(days = 1)
    if dateta < time_now:  # check if stored time exceeds 1 day
        product_v.delete(keep_parents = True)

我想把SESSION_COOKIE_AGE设为1个月,但是会影响product_views。 1天后就不能算了,1个月后才算,那不行。我不知道,它会像默认的 DB Session 引擎一样好用吗?你有什么想法吗?如果有任何帮助,我将不胜感激。

我找到了解决方案。没必要做2nd Session Engine,因为要花很多时间,到时候编辑起来也很困难。 这是一个更简单的方法: 在 middleware.py 我输入:

class ViewsMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response
        # One-time configuration and initialization.

    def __call__(self, request):
        check_view(request)

        response = self.get_response(request)

        # Code to be executed for each request/response after
        # the view is called.

        return response


def check_view(request):
    now = datetime.datetime.today()
    print(request.session['viewed'])
    print(request.session['views'])
    if 'viewed' not in request.session or request.session['viewed'] != now.day:
        request.session['viewed'] = now.day
        request.session['views'] = []

        obj, created = SiteView.objects.get_or_create(
                current = f'{now.year} {now.month} {now.day}',
                defaults = {'current': f'{now.year} {now.month} {now.day}', 'views': 1}
        )
        if not created:
            obj.views += 1
            obj.save()


并且在 details_view(查看产品时):

def detail_view(request, id):
    obj = get_object_or_404(Product, pub_id = id)
    if 'views' not in request.session:
        request.session['views'] = []
    arr = request.session['views']
    if id not in arr:
        arr.append(id)
        request.session['views'] = arr
        obj.product_views = obj.product_views + 1
        obj.save()

    context = {
        'title' : obj.title,
        'object': obj,
    }
    return render(request, 'shop/products/detail.html', context)

我希望,你会的。