When I try to use DetailView to view my posts I keep getting an exception error.
ImproperlyConfigured at /musician_details/1/
DetailView is missing a QuerySet. Define DetailView.model, DetailView.queryset, or override DetailView.get_queryset().
Request Method: GET
Request URL: http://127.0.0.1:8000/musician_details/1/
Django Version: 3.2.5
Exception Type: ImproperlyConfigured
Exception Value:
DetailView is missing a QuerySet. Define DetailView.model, DetailView.queryset, or override DetailView.get_queryset().
Exception Location: C:\Program Files\Python38\lib\site-packages\django\views\generic\detail.py, line 69, in get_queryset
Python Executable: C:\Program Files\Python38\python.exe
Python Version: 3.8.2
Python Path:
['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']
Server time: Sun, 19 Sep 2021 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 %}
Your DetailView URL pattern in urls.py
is wrong.
You need to use /<int:pk>/
not /<pk>/
.
You also used DetailView
so fix it to be MusicianDetail
. That is, You call your musician detail view MusicianDetail
, so in order to have valid URLs, you have to use MusicianDetail
in your urls.py
. DetailView
is nothing but parent class to create detail view in the Django projects.
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('musician_details/<int:pk>/', views. MusicianDetail.as_view(), name='musician_details'),
]