【问题标题】:Django DetailView template not showing the detailsDjango DetailView 模板未显示详细信息
【发布时间】:2020-02-22 15:01:44
【问题描述】:

过去几天我正在学习 Django Web 框架,它非常棒。我正在学习基于类的视图来显示内容。我创建了一个简单的示例模型school(name,principal,location)student(name,age,school(foreign key))。模型是

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

我的 urls.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')
]

主 urls.py

from django.contrib import admin
from django.urls import path, include
from CBV_app import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',views.IndexView.as_view()),
    path('CBV_app/',include('CBV_app.urls', namespace='CBV_app'))
]

学生模型中的外键有一个与学校模型连接的related_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_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 %}

我在我的 school_list.html 中创建了两个链接,用于打开相应的学校详细信息页面并显示内容。 打开详细信息页面后,我无法查看相应学校的学生详细信息。我检查了很多次文件,但无法找出问题所在。我附上了No. of students in the school modelSchool Details Page的图片供参考。

谁能帮我解决这个问题? 提前致谢。

【问题讨论】:

  • 可能这所学校没有学生。
  • 你能(a)正确缩进你的代码吗? (b) 提供您的模型的相关细节; (c) 显示您传递的 URL; (d) 确保您的School 有相关学生?
  • 我已经用相关细节编辑了问题,并检查了数据库中是否存在学生详细信息

标签: python django


【解决方案1】:

首先,您的Student 模型的相关名称中有错字:您输入的相关名称是“stduents”,但在您的模板中您使用的是“students”。

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

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

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

在您的模板中。

【讨论】:

    猜你喜欢
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 2020-12-06
    • 2021-12-29
    相关资源
    最近更新 更多