python django 中没有显示点赞数......我不知道为什么......有人可以帮助我

count of likes is not showing in python django .... I don't know why .. Can someone help me

注册和登录页面工作正常但我的点赞数没有显示在 django 中...我不知道为什么...有人可以帮我找到这个错误...这将是伟大的求助...谢谢!!

1.Views.py

             from django.shortcuts import render, get_object_or_404, redirect
from datasecurity.models import Post
from django.urls import reverse
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required


# Create your views here.
@login_required
def likes(request, pk):
    post=get_object_or_404(Post, pk=pk)
    post.likes.add(request.user)
    return HttpResponseRedirect(reverse('datasecurity:datasecurity'))
def datasecurity(request):
    allPosts= Post.objects.all()

    context={'allPosts': allPosts}
    def __init__(self, *args, **kwargs):
        stuff = get_object_or_404(Post, id = self.kwargs['pk'])
        total_likes = stuff.total_likes()
        context['total_likes'] = total_likes



    return render(request, 'datasecurity/data.html',context=context)

def blogHome(request, slug):
    post=Post.objects.filter(slug=slug).first()
    context={"post":post}
    return render(request, "datasecurity/blogHome.html", context)

2.Urls.py

from django.conf.urls import url

from . import views

app_name = 'datasecurity'

urlpatterns = [
    url(r'^$', views.datasecurity, name="datasecurity"),
    url(r'^datasecurity/(?P<slug>[^/]+)', views.blogHome, name='blogHome'),
    url(r'^likes/(?P<pk>\d+)/', views.likes, name = "likes"),

]

3.models.py

from django.db import models
from ckeditor.fields import RichTextField
from django.contrib.auth.models import User
# Create your models here.
class Post(models.Model):
    sno=models.AutoField(primary_key=True)
    title=models.CharField(max_length=255)
    author=models.CharField(max_length=14)
    slug=models.CharField(max_length=130)
    timeStamp=models.DateTimeField(blank=True)
    content=RichTextField(blank=True, null=True)
    img = models.ImageField(blank=True, null=True, upload_to="dataimage/")
    likes = models.ManyToManyField(User)

    @property
    def total_likes(self):
        return self.likes.count()

    def __str__(self):
        return self.title + " by " + self.author

4.data.html

<!DOCTYPE html>
{% load static %}
<html lang="en" dir="ltr">
  <head>
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
    />

    <link
      href="https://fonts.googleapis.com/css?family=Lato:100,300,400,700,900"
      rel="stylesheet"
    />

    <meta charset="utf-8">
    <title></title>
    <!-- Bootstrap core CSS -->
    <link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">


    <link rel="stylesheet" href="{% static 'css/datasecurity.css' %}">
    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css2?family=PT+Sans&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Bebas+Neue&family=PT+Sans&display=swap" rel="stylesheet">
    <link rel="canonical" href="https://getbootstrap.com/docs/5.0/examples/cover/">
    <script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v9.0" nonce="noEcH88O"></script>
    <script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v3.2"></script>
  </head>
  <body>

    <!-- End header -->
    <div id="page-wraper">

      <!-- Sidebar Menu -->
      <div class="responsive-nav">
        <div class="top-navbar">
        <nav class="navbar navbar-expand-lg navbar-light bg-light">

          <div class="container">


              <marquee><ul class="navbar-nav ml-auto">

                <li class="nav-item"><a class="nav-link" href="{% url 'careforallapp:base' %}">Home</a></li>


              </ul></marquee>
            </div>
          </div>
        </nav>
      </div>
      <section class="section about-me" data-section="section1">
        <div class="container">
          <div class="section-heading">
            <h2>Welcome to Data Security</h2>


    {% for post in allPosts  %}

            <div class="line-dec"></div>
            <span
              >This is a Bootstrap v4.2.1 CSS Template for you. Edit and use
              this layout for your site. Updated on 21 May 2019 for repeated main menu HTML code.</span
            >
          </div>
          <div class="left-image-post">
            <div class="row">
              <div class="col-md-6">
                <div class="left-image">
                  {% if post.img %}
                    <img src="{{ post.img.url }}" alt="" />
                  {% endif %}
                </div>
              </div>
              <div class="col-md-6">
                <div class="right-text">
                  <h4>{{post.title}}</h4>
                  <h6>Article  by {{post.author}}</h6>
                  <h2>{{post.datetime}}</h2>

                  <p>
                    {{post.content|safe | truncatechars:280}}
                  </p>

                  <form action = "{% url 'datasecurity:likes' pk=post.pk %}" method = "POST">
                    {% csrf_token %}
                    <button type="submit" name="post_id" value = "post_id" class="btn"> Like </button> - {{ total_likes }} likes
                  </form><br><br>

                  <div class="white-button">
                    <a href="{% url 'datasecurity:blogHome' slug=post.slug %}">Read More</a>
                  </div><br>

                </div>

              </div>
            </div>
          </div>

            </div>
          </div>
        </div>
      </section>


      </div>
        {% endfor %}
    </div>
    <div>
    </section>
  </div>

我调试代码时没有错误..可能是逻辑错误..我真的不知道..谁能帮我找出错误谢谢

您似乎想在 datasecurity 视图中显示所有帖子。您出于某种原因在其中声明了一个函数 __init__(甚至没有使用它,它甚至不在 class 中,因为 __init__ 可能暗示)。像这样更改视图:

def datasecurity(request):
    allPosts= Post.objects.all()
    context={'allPosts': allPosts}
    return render(request, 'datasecurity/data.html',context=context)

现在在您的模板中您正在使用 {{ total_likes }} 但您没有在上下文中传递任何此类变量。但是我注意到你已经在你的模型上声明了一个方法,所以将它更改为 {{ post.total_likes }}.