如何修复 Django DetailView Missing query set 异常错误

How to fix Django DetailView Missing query set exception error

当我尝试使用 DetailView 查看我的帖子时,我不断收到异常错误。

ImproperlyConfigured at /post/1/ BlogDetailView is missing a QuerySet. Define BlogDetailView.model, BlogDetailView.queryset, or override BlogDetailView.get_queryset(). Request Method: GET Request URL: http://127.0.0.1:8000/post/1/ Django Version: 2.2 Exception Type: ImproperlyConfigured Exception Value:
BlogDetailView is missing a QuerySet. Define BlogDetailView.model, BlogDetailView.queryset, or override BlogDetailView.get_queryset(). Exception Location: C:\Users\julia.virtualenvs\Documents-SYi_ANcG\lib\site-packages\django\views\generic\detail.py in get_queryset, line 73 Python Executable: C:\Users\julia.virtualenvs\Documents-SYi_ANcG\Scripts\python.exe Python Version: 3.7.3

我已经根据 Will Vicent 的 Django 初学者一书检查了我的代码,但我仍然找不到任何问题

models.py

from django.db import models

# Create your models here.

class Post(models.Model):

    title = models.CharField(max_length=200)

    author = models.ForeignKey(

        'auth.User',

        on_delete=models.CASCADE,

    )
    body = models.TextField()

    def __str__(self):

        return self.title

views.py

from django.views.generic import ListView, DetailView # new
from .models import Post

# Create your views here.

class BlogListView(ListView):

    model = Post

    template_name = 'home.html'

class BlogDetailView(DetailView): # new

    Model = Post

    template_name = 'post_detail.html'

urls.py

# blog/urls.py
from django.urls import path
from .views import BlogListView, BlogDetailView # new

urlpatterns = [

    path('post/<int:pk>/', BlogDetailView.as_view(), name='post_detail'), # new

    path('', BlogListView.as_view(), name='home'),

]

post_detail.html

<!-- templates/post_detail.html-->
{% extends 'base.html' %}

{% block content %}
    <div class="post-entry">
        <h2>{{ post.title }}</h2>
        <p>{{ post.body }}</p>
    </div>

{% endblock content %}

此代码应该允许我在浏览到 http://127.0.0.1/posts/1 或 post/2

时看到我的帖子

您的 DetailView 中有一个简单的拼写错误:Model 而不是 model