由于某种原因,我的模型数据未显示在我的视图中

For some reason my modle data is nto displying in my view

我从事博客项目已有一段时间了,运行 遇到了一个大问题。当我转到特定博客 posts 页面时,该页面的数据未加载。这不是 URL 的问题,因为 URL 对于每个博客 post 都是唯一的。任何帮助将不胜感激。

观看次数

from django.shortcuts import render, reverse
from django.http import HttpResponseRedirect
from django.views import generic
from . import models
from django.contrib.auth import get_user_model
User = get_user_model()
from django.urls import reverse_lazy
from django.contrib.auth.mixins import LoginRequiredMixin
# Create your views here.

class create_blog_post(generic.CreateView, LoginRequiredMixin):
    model = models.Blog_Post
    template_name = 'blog_app/creat_post.html'
    fields = ('post_title', 'blog_content')
    success_url = reverse_lazy('blog_app:all')

class view_blog_post(generic.TemplateView):
    model = models.Blog_Post
    template_name = 'blog_app/view_post.html'

class delet_blog_post(generic.DeleteView, LoginRequiredMixin):
    model = models.Blog_Post
    template_name = 'blog_app/delete_post.html'

    def get_queryset(self):
        queryset = super().get_queryset()
        return queryset.filter(user_id = self.request.user.id)

    def delete(self, *args, **kwargs):
        messages.success(self.request, 'Post Deleted')
        return super().delete(*args, *kwargs)

class all_blog_posts(generic.ListView):
    model = models.Blog_Post
    template_name = 'blog_app/all_posts.html'
    #slug_url_kwarg = "slug"

modles.py

from django.db import models
from taggit.managers import TaggableManager

# Create your models here.
class IpModel(models.Model):
    ip = models.CharField(max_length=100)

    def __str__(self):
        return self.ip

class Blog_Post(models.Model):
    slug = models.SlugField(max_length=1000, editable=False, null=True)
    post_title = models.CharField(max_length=100, editable=True, blank=False, null=True)
    blog_content = models.TextField(max_length=10000, blank=False, editable=True, null=True)
    date = models.DateTimeField(blank=False, null=True, auto_now=True, editable=False)
    likes = models.ManyToManyField(IpModel, related_name="post_likes", blank=True)

    def save(self, *args, **kwargs):
        self.slug = self.slug
        super().save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse("mypage_detail", kwargs={"pk": self.pk})

view_post.html

{% extends "base.html" %}

{% block content %}
<div class="jumbotron">
  {{blog_post.post_title}}
  <p>{{blog_post.blog_content}}</p>
</div>
{% endblock %}

您正在使用 TemplateView 而不是 DetailView,它实际上默认使用模型方法。

class view_blog_post(generic.DetailView):
    model = models.Blog_Post
    ...