Python Django DetailView模板未显示详细信息

Python Django DetailView模板未显示详细信息,python,django,Python,Django,在过去的几天里,我学习了DjangoWeb框架,它非常棒。我正在学习基于类的视图来显示内容。我创建了一个简单的示例模型学校(姓名、校长、地点)和学生(姓名、年龄、学校(外键))。模型是 from django.db import models # Create your models here. # Create a School model with different classes # School Model with Name,Pricipal & Location cla

在过去的几天里,我学习了DjangoWeb框架,它非常棒。我正在学习基于类的视图来显示内容。我创建了一个简单的示例模型
学校(姓名、校长、地点)
学生(姓名、年龄、学校(外键))
。模型是

from django.db import models

# Create your models here.

# Create a School model with different classes
# School Model with Name,Pricipal & Location
class School(models.Model):

    # Create name,pricipal,location fields for the School
    name = models.CharField(max_length=256)
    principal = models.CharField(max_length=256)
    location = models.CharField(max_length=256)

    # method to pritn the string representation of the class
    def __str__(self):
        return self.name

# Create a stduent model of the school
class Student(models.Model):

    # create name,age,school fields for students
    name = models.CharField(max_length=256)
    age = models.PositiveIntegerField()
    school = models.ForeignKey(School, related_name='stduents', 
                                            on_delete=models.CASCADE)

    # method to print the string representation of stduent class
    def __str__(self):
        return self.name
我的URL.py是

from django.urls import path
from . import views

# Create a app_name for template tagging
app_name = 'CBV_app'

urlpatterns = [
     path('',views.SchoolListView.as_view(),name='list'),
     path('<int:pk>/',views.SchoolDetailView.as_view(),name='detail')
]
学生模型中的外键有一个与学校模型连接的相关的\u name='students'。我已经在
views.py
文件中注册了模型并创建了基于类的视图(列表和详细视图)。
views.py
文件是

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

# Create your views here.



# create a view using the built in template view
class IndexView(TemplateView):

   # template_name is the class object attribute of class TemplateView
   # Just pass in the html file that need to be displayed
   template_name = 'index.html'


# create a list view class for school inheriting from ListView
class SchoolListView(ListView):


    context_object_name = 'schools'


    # connect this to the created models
    # this provides the models each record in the form of list
    model = models.School


# create a detail view for the school by inheriting the DetailView
class SchoolDetailView(DetailView):

    context_object_name = 'school_detail'

    # set the view to the model
    model = models.School
    # point the class attribute template_name to point the detail view

    template_name = 'CBV_app/school_detail.html'
我的
student\u detail.html
文件是

{% extends "CBV_app/CBV_app_base.html" %}

{% block body_block %}

<div class="jumbotron">
<p class="display-4">Welcome to school details page</p>
<p class="h3">School details:</p>
<p class="lead">School Id: {{school_detail.id}}</p>
<p class="lead">Name: {{school_detail.name}}</p>
<p class="lead">Principal: {{school_detail.principal}}</p>
<p class="lead">Location: {{school_detail.location}}</p>
<h3 class="h3">Student Details:</h3>
  <!-- students is the related name given in the model with the foreign key -->
  <!-- which connects with the school model -->
  {% for stu in student_detail.students.all %}
    <p>{{stu.name}} who is {{stu.age}} years old</p>
  {% endfor %}
</div>

{% endblock %}
{%extensed“CBV_app/CBV_app_base.html”%}
{%block body_block%}

欢迎来到学校详情页面

学校详情:

学校Id:{{School\u detail.Id}

名称:{{school\u detail.Name}

校长:{{school\u detail.Principal}

位置:{{school\u detail.Location}

学生资料: {stu detail.students.all%} {{stu.name}}谁{{stu.age}岁

{%endfor%} {%endblock%}
我在my school_list.html中创建了两个链接,打开相应的学校详细信息页面并显示内容。 打开详细信息页面后,我无法查看相应学校的学生详细信息。我检查了很多次文件,但都没能找出故障所在。我已附上和的图片以供参考

有人能帮我解决这个问题吗?
提前感谢。

首先,您的
学生
模型的相关名称中有一个输入错误:您在模板中输入的相关名称是“stduents”,但您在模板中使用的是“students”

其次,您在模板中的错误(或不存在)模型上使用了相关名称:Change

{% for stu in student_detail.students.all %}


在你的模板中。

也许这所学校没有学生。你能(a)正确缩进你的代码吗;(b) 提供模型的相关细节;(c) 显示您传递的URL;以及(d)确保有与您的
学校相关的学生
?我已使用相关详细信息编辑了问题,并检查了数据库中是否存在学生详细信息
{% for stu in student_detail.students.all %}
{% for stu in school_detail.students.all %}