过去几天,我正在学习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 model和School Details Page的图片以供参考。
有人可以帮助我解决此问题吗? 预先感谢。
答案 0 :(得分:0)
首先,您的Student
模型的相关名称中有一个错字:您在其中输入的相关名称是“ stduents”,但在模板中您使用的是“ students”。
第二,您在模板中的错误(或不存在)模型上使用了相关名称:更改
{% for stu in student_detail.students.all %}
到
{% for stu in school_detail.students.all %}
在您的模板中。