如何修复 DetailView 缺少 QuerySet 错误?

How to fix DetailView is missing a QuerySet Error?

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

在 /musician_details/1/ 处配置不当 DetailView 缺少 QuerySet。定义 DetailView.model、DetailView.queryset 或覆盖 DetailView.get_queryset()。 请求方式:GET 请求 URL:http://127.0.0.1:8000/musician_details/1/ 姜戈版本:3.2.5 异常类型:配置不当 异常值:
DetailView 缺少 QuerySet。定义 DetailView.model、DetailView.queryset 或覆盖 DetailView.get_queryset()。 异常位置:C:\Program Files\Python38\lib\site-packages\django\views\generic\detail.py, line 69, in get_queryset Python 可执行文件:C:\Program Files\Python38\python.exe Python版本:3.8.2 Python路径:
['F:\Full Stack\Django\Django Project\Test', 'C:\Program Files\Python38\python38.zip', 'C:\Program Files\Python38\DLLs', 'C:\Program Files\Python38\lib', 'C:\Program Files\Python38', 'C:\Users\Tanim\AppData\Roaming\Python\Python38\site-packages', 'C:\Program Files\Python38\lib\site-packages', 'C:\Program Files\Python38\lib\site-packages\win32', 'C:\Program Files\Python38\lib\site-packages\win32\lib', 'C:\Program Files\Python38\lib\site-packages\Pythonwin'] 服务器时间:2021年9月19日星期日07:44:37 +0000

views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic import View, TemplateView, ListView, DetailView
from home import models


class IndexView(ListView):
  

    context_object_name = 'musician_list'
    model = models.Musician
    template_name = 'first_app/index.html'


class MusicianDetail(DetailView):
    context_object_name = 'musician'
    model = models.Musician
    template_name = 'first_app/musician_details.html'

models.py

from django.db import models

# Create your models here.       
class Musician(models.Model):    
    first_name=models.CharField(max_length=50)
    last_name=models.CharField(max_length=50)
    instrument=models.CharField(max_length=50)

    def __str__(self):
        return self.first_name + " " + self.last_name        
   
class Album(models.Model):
    artist=models.ForeignKey(Musician, on_delete=models.CASCADE)
    name=models.CharField(max_length=100)
    release_date=models.DateField()    
    rating=(
    (1,"Worst"),
    (2,"Bad"),
    (3,"Not Bad"),
    (4,"Good"),
    (5,"Excellent!"),    
    )        
    num_Stars=models.IntegerField(choices=rating)
          
    def __str__(self):
        return self.name +", Rating: " +str(self.num_Stars)
    

models.py

from django.contrib import admin
from django.urls import path
from home import views

app_name = "home"

urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('musician_details/<pk>/', views.DetailView.as_view(), name='musician_details'),    
]

index.html

{% extends 'first_app/base.html' %}
{% block body_block %}

<ul>

{% for musician in musician_list %}

<li><a href="{% url 'home:musician_details' pk=musician.pk %}">{{musician.first_name}} {{musician.last_name}}</a> </li>  

{% endfor %}

</ul>

{% endblock %}  

musician_details.html

{% extends 'first_app/base.html' %}
{% block body_block %}

<h3>Musician Details:</h3>
<p>{{musician.first_name}} {{musician.last_name}}</p>
<p>{{musician.instrument}}</p>

{% endblock %} 

您在 urls.py 中的 DetailView URL 模式是错误的。

您需要使用 /<int:pk>/ 而不是 /<pk>/

您还使用了 DetailView,因此将其修改为 MusicianDetail。也就是说,您调用音乐家详细信息视图 MusicianDetail,因此为了获得有效的 URL,您必须在 urls.py 中使用 MusicianDetailDetailView 只是在 Django 项目中创建详细视图的父级 class。

urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('musician_details/<int:pk>/', views. MusicianDetail.as_view(), name='musician_details'),    
]

模特=音乐家 下班后 和 你做 urls.py a int:pk 然后工作正常

views.py:

class MusicianDetail(DetailView):
    context_object_name = 'musician'
    model = Musician
    template_name = 'first_app/musician_details.html'

urls.py:

urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('musician_details/<int:pk>/', views.MusicianDetail.as_view(), 
name='musician_details'),    
]