跳转到主要内容

django-cacheback 和 django-hitcount 之间的简单集成

项目描述

django-popularity-mixin

django-cacheback 和 django-hitcount 之间的简单集成 django-cachebackdjango-hitcount

作者: Basil Shubin

https://img.shields.io/pypi/v/django-popularity-mixin.svg https://img.shields.io/pypi/dm/django-popularity-mixin.svg https://img.shields.io/github/license/bashu/django-popularity-mixin.svg https://img.shields.io/travis/bashu/django-popularity-mixin.svg

需求

您必须安装并配置 django-cachebackdjango-hitcount,有关详细信息及设置说明,请参阅 django-cachebackdjango-hitcount 文档。

安装

首先安装模块,最好是在虚拟环境中安装。它可以从PyPI安装

pip install django-popularity-mixin

设置

确保项目已配置为 django-cachebackdjango-hitcount

然后添加以下设置

INSTALLED_APPS += (
    'popularity',
)

使用

可以使用 popularity.views.PopularityMixin 通过设置 count_hitTrue 来异步执行计数击打的业务逻辑。

# views.py

from django.views.generic.detail import DetailView

from popularity.views import PopularityMixin


class CustomDetailView(PopularityMixin, DetailView):
    count_hit = True    # set to True if you want it to try and count the hit asynchronously
    template_name = "template.html"
    ...

popularity.viwes.PopularityMixin 扩展了 Django 的通用 django.views.generic.detail.DetailView 并注入了一个额外的上下文变量 hitcount

<!-- template.html -->

{# the primary key for the hitcount object #}
{{ hitcount.pk }}

{# the total hits for the object #}
{{ hitcount.total_hits }}

为了更细致地查看相关对象的命中次数,您可以使用 get_hit_count 模板标签。

{# remember to load the tags first #}
{% load popularity_tags %}

{# Return total hits for an object: #}
{% get_hit_count for [object] %}

{# Get total hits for an object as a specified variable: #}
{% get_hit_count for [object] as [var] %}

{# Get total hits for an object over a certain time period: #}
{% get_hit_count for [object] within ["days=1,minutes=30"] %}

{# Get total hits for an object over a certain time period as a variable: #}
{% get_hit_count for [object] within ["days=1,minutes=30"] as [var] %}

请参阅 示例 应用程序。此应用程序用于手动测试该包的功能。这也是一个好例子...

您需要 Django 1.8.1 或更高版本来运行它。它可能在旧版本上运行,但未经过测试。

贡献

如果您喜欢这个模块,已经分叉了它,或者想改进它,请让我们知道!也欢迎提交拉取请求。:-)

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源分发

django-popularity-mixin-0.1.8.tar.gz (8.6 kB 查看哈希值)

上传时间

构建分发

django_popularity_mixin-0.1.8-py3-none-any.whl (10.6 kB 查看哈希值)

上传时间 Python 3

支持者

AWSAWS 云计算和安全赞助商 DatadogDatadog 监控 FastlyFastly CDN GoogleGoogle 下载分析 MicrosoftMicrosoft PSF赞助商 PingdomPingdom 监控 SentrySentry 错误记录 StatusPageStatusPage 状态页面