0

我确实在我的博客上实现了 django-hitcount。我可以进行视图计数工作,但在我的管理页面中访问时发生错误。按照代码和错误图像:谢谢。

  • 网址.py
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path, include
from blog.views import index, blog, post  


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', index, name='home'),
    path('blog/', blog, name='post-list'),
    path('post/<id>/', post, name='post-detail'),
    path('ckeditor', include('ckeditor_uploader.urls')),
    path('hitcount/', include(('hitcount.urls', 'hitcount'), namespace='hitcount')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  • 视图.py
from django.shortcuts import render, get_object_or_404, redirect, reverse
from .models import Post, Author
from django.db.models.aggregates import Count
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from hitcount.models import HitCount
from hitcount.views import HitCountMixin

...

def post(request, id):
    category_count = get_category_count()
    most_recent = Post.objects.order_by('-timestamp')[:3]
    popular_posts = Post.objects.order_by('-hit_count_generic__hits')[:3]
    post = get_object_or_404(Post, id=id)
    hit_count = HitCount.objects.get_for_object(post)
    hit_count_response = HitCountMixin.hit_count(request, hit_count)
    count_hit = True

    if request.method == "POST":
        return redirect(reverse("post-detail", kwargs={
            'id': post.pk
        }))

    context = {
        'post': post,
        'hit_count': hit_count,
        'category_count': category_count,
        'popular_posts': popular_posts,
        'views': Post.objects.get(pk=1),
        # views.hit_count.hits
        # views.hit_count.hits_in_last(days=7)
    }
    return render(request, 'blog/post.html', context)
  • post.html
{% extends 'blog/base.html' %}
{% load static %} 
{% load hitcount_tags %}
{% block content %}
<body>
<div class="container">
    <div class="row">
        <div class="col-md-8">
            <div>

                <div class="" >
                    <div class="card mb-3">
                    <img src="{{ post.thumbnail.url }}" class="card-img-top with:"100%"" alt="...">

                    <div class="card-body">
                        <h5 class="card-title">{{ post.title }}</h5>
                        <img src="{{ post.author.profile_picture.url}}" alt="..." class="rounded-circle width="40" height="40""  >
                        <p>{{ post.author }} | <i class="far fa-clock"></i>  {{ post.timestamp|timesince }} ago | <i class="far fa-eye"> {% get_hit_count for post %}  views</i></p>
                        <div>
                        <hr class="my-4">
                        <p class="card-text">{{ post.content|safe }}</p>
                        </div>
                        <p class="card-text"><small class="text-muted">{{ post.timestamp}}</small></p>
                    </div>
                    </div>
                </div>

            </div>
        </div>
<!--Side Bar-->
        {% include 'blog/sidebar.html' with most_recent=most_recent category_count=category_count %}
    </div>
</div>
</body>

在此处输入图像描述

4

0 回答 0