Ext.: http://www.codingsoho.com/zh/blog/component-hitcount/
Hit counter is used to count the number of accesses to a model object.
Django Hit counter application This tracks the number of Hits/views for chosen objects.
Https://github.com/thornomad/django-hitcount
Installation
Pip Install Django-hitcount
Reference website http://django-hitcount.readthedocs.org/en/latest/installation.html
Configuration
Add Django-hitcount to Installed_apps while activating session_save_every_request
settings.py
INSTALLED_APPS = ( ... ‘hitcount‘)# needed for django-hitcount to function properlySESSION_SAVE_EVERY_REQUEST = True
More Configuration View http://django-hitcount.readthedocs.org/en/latest/settings.html
View
Join in urls.py
urlpatterns = patterns(‘‘, ... url(r‘hitcount/‘, include(‘hitcount.urls‘, namespace=‘hitcount‘)),)
Record clicks
Https://django-hitcount.readthedocs.io/en/latest/installation.html#counting-hits
can be used HitCountMixin or the following two views
HitCountJSONView: a JavaScript implementation which moves the business-logic to an Ajax View and hopefully speeds up page load times and E Liminates some bot-traffic
HitCountDetailView: which provides a wrapper from Django's generic DetailView and allows you to process the hit as the view is loaded
My implementation project has a Class View and a function view, using the above Hitcountmixin and Hitcountdetailview two methods
Hitcountmixin
Mixin can be used for the CBV class or for a class method that calls it directly hit_count() . The method has two parameters HttpRequest and HitCount objects that return a tuple of names UpdateHitCountResponse(hit_counted=Boolean, hit_message=‘Message‘) .
If the click is logged, then hit_counted=True false. hit_message Used to indicate whether the current click is counted or ignored.
This mixin can is used in your own class-based views or you can call the Hit_count () class method directly. The method takes the arguments, a HttpRequest and HitCount object It'll return a namedtuple:updatehitcountresponse (hit_ Counted=boolean, hit_message= ' message '). Hit_counted would be True if the hit is counted and False if it is not. Hit_message would indicate by what means the hits was either counted or ignored.
The code style is as follows
from hitcount.models import HitCountfrom hitcount.views import HitCountMixin# first get the related HitCount object for your model objecthit_count = HitCount.objects.get_for_object(your_model_object)# next, you can attempt to count a hit and get the response# you need to pass it the request object as wellhit_count_response = HitCountMixin.hit_count(request, hit_count)# your response could look like this:# UpdateHitCountResponse(hit_counted=True, hit_message=‘Hit counted: session key‘)# UpdateHitCountResponse(hit_counted=False, hit_message=‘Not counted: session key has active hit‘)
Hitcountdetailview
By setting count_hit=True The HitCountDetailView business logic that can be used to calculate the number of clicks.
The Hitcountdetailview can is used to does the business-logic of counting the hits by setting Count_hit=true. See the view sections for more information on what else was added to the template context with this
Here is the implementation in the library example
from hitcount.views import HitCountDetailViewclass PostCountHitDetailView(HitCountDetailView): model = Post # your model goes here count_hit = True # set to True if you want it to try and count the hit
Show Click Count
There are several ways to implement the count display Displaying-hits
- Template Tags:provide a robust to get related counts
- Views:allows you to wrap a class-based view and inject additional context into your template
- Models:can a generic relation to their respective HitCount
I chose the first method in the project, which avoids modifying the background code. Concrete implementation See back
# remember to load the tags first{% load hitcount_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] %}
Instance item textcourse through Hitcount record views
from hitcount.models import HitCountfrom hitcount.views import HitCountMixinhit_count = HitCount.objects.get_for_object(node)# next, you can attempt to count a hit and get the response# you need to pass it the request object as wellhit_count_response = HitCountMixin.hit_count(request, hit_count)# your response could look like this:# UpdateHitCountResponse(hit_counted=True, hit_message=‘Hit counted: session key‘)# UpdateHitCountResponse(hit_counted=False, hit_message=‘Not counted: session key has active hit‘) context = { ‘object‘: node, } context.update(hit_count_response._asdict())
hit_count_response._asdict()The result is hit_counted andhit_message
Show number of views
Load tags at the beginning of a page where you need to record clicks
{% load hitcount_tags %}
I just need a simple count, so I'll use the following.
{% get_hit_count for object %}
Aldryn_newsblog
This is the Djangocms plugin, which is implemented by modifying the original code.
There is no way to find the solution only by adding code, now the plan to change the library, very inconvenient.
One way to the front, modify the file
aldryn_newsblog\views.py
from hitcount.models import HitCountfrom hitcount.views import HitCountMixinclass ArticleDetail(AppConfigMixin, AppHookCheckMixin, PreviewModeMixin, TranslatableSlugMixin, TemplatePrefixMixin, DetailView): def get(self, request, *args, **kwargs): hit_count = HitCount.objects.get_for_object(self.object) hit_count_response = HitCountMixin.hit_count(request, hit_count) ...
Or we can use it HitCountDetailView to implement, refer to Https://django-hitcount.readthedocs.io/en/latest/installation.html#hitcountdetailview
aldryn_newsblog\views.py
from hitcount.views import HitCountDetailViewclass ArticleDetail(AppConfigMixin, AppHookCheckMixin, PreviewModeMixin, TranslatableSlugMixin, TemplatePrefixMixin, HitCountDetailView, DetailView):...... count_hit = True
Then modify the template file similarly
Aldryn_newsblog\includes\article.html
{% load hitcount_tags %} <i class="fa fa-eye" style="font-size: smaller;"> {% get_hit_count for object %} 浏览</i>
Page view statistics (i) Hitcount