Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

html - Blog Post's hit/views count are not showing

I am going through a Django tutorial. In my Blog App, i am trying to count the views/hit counts in the blog post.

views.py

def detail_view(request,id):
    data = get_object_or_404(BlogPost,pk=id)
    count_hit = True


    if request.method == 'POST':
        comment_form = CommentForm(data=request.POST)
        if comment_form.is_valid()

    else:
        comment_form = CommentForm()

    context = {'data':data,'count_hit':count_hit}
    return render(request, 'mains/blog_post_detail.html', context )

urls.py

path('hitcount/', include(('hitcount.urls', 'hitcount'), namespace='hitcount')),

blog_post_detail.html

<a>Title : {{ data.post_title }}</a>

<p>Views: {% get_hit_count for data %}</p>

models.py

class BlogPost(models.Model):
    post_creater = models.ForeignKey(User,default='',null=True,on_delete = models.CASCADE)
    post_title = models.CharField(max_length=500,default='')
    date_added = models.DateTimeField(auto_now_add=True,null=True)
    hit_count_generic = GenericRelation(HitCount,object_id_field='object_pk',related_query_name='hit_count_generic_relation')

def __str__(self):
    return self.post_title

The Problem

I have put the hit/views count in blog_post_detail.html. BUT when i open post in browser , It shows ---Views:0.

What i have tried

1). I have applied all the migrations.

2). I have inserted the hitcount app in INSTALLED_APPS in settings.py

Help me in this ! I will really appreciate your Help. Thank You In advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...