******我有一个课程类,我制作了一个索引函数来检索模板中的所有课程。然后每个特定的课程都有其主题,然后我创建一个功能course_content并在details.html中检索每个课程的主题,直到现在我已经完成了任务,现在我想获取该特定主题的主题小节,章节,主题的详细信息。 html模板******
class Institute(models.Model):
institute_name = models.CharField(max_length=100, null=True, blank=True)
institute_logo = models.FileField(upload_to='pic',null=True, blank=True)
institute_code = models.IntegerField(blank=True, null=True)
def __str__(self):
return self.institute_name
SEM_NO_CHOICES = (
('1', '1'),
('2', '2'),
('3', '3'),
('4', '4'),
('5', '5'),
('6', '6'),
('7', '7'),
('8', '8'),
('9', '9'),
('10', '10'),
('11', '11'),
('12', '12')
)
COURSE_DURATION_CHOICES =
('sem', 'Semester'),
('year', 'Year')
)
YEAR_NO_CHOICES =
('1', '1'),
('2', '2'),
('3', '3'),
('4', '4')
)
class Course(models.Model):
course_name = models.CharField(max_length=100, null=True, blank=True)
course_code = models.IntegerField(null=True, blank=True)
institute = models.ForeignKey(Institute, on_delete=models.CASCADE, null=True, blank=True)
duration = models.CharField(choices=COURSE_DURATION_CHOICES, max_length = 10, null=True, blank=True, default="")
sem_no = models.CharField(choices = SEM_NO_CHOICES, max_length = 10, blank = True, null = True, default="")
year_no = models.CharField(choices = YEAR_NO_CHOICES, max_length = 10, blank = True, null = True, default = "")
content = models.TextField(null=True, blank=True)
is_live = models.BooleanField(default=True)
def __str__(self):
return '%s' %(self.course_name)
class Branch(models.Model):
branch_name = models.CharField(max_length=50, null=True, blank=True)
branch_code = models.IntegerField(null=True, blank=True)
course = models.ForeignKey(Course, on_delete=models.CASCADE, null=True, blank=True)
institute = models.ForeignKey(Institute, on_delete=models.CASCADE, null=True, blank=True)
def __str__(self):
return '%s' %(self.branch_name)
class Subject(models.Model):
institute = models.ForeignKey(Institute, on_delete=models.CASCADE, null=True, blank=True)
course = models.ForeignKey(Course, on_delete=models.CASCADE, null=True, blank=True)
branch = models.ForeignKey(Branch, on_delete=models.CASCADE, null=True, blank=True)
sem_no = models.CharField(choices = SEM_NO_CHOICES, max_length=10, blank=True, null=True, default="")
year_no = models.CharField(choices = YEAR_NO_CHOICES, max_length=10, blank=True, null=True, default="")
subject_name = models.CharField(max_length=250, blank=True, null=True)
subject_code = models.IntegerField(blank=True, null=True)
is_live = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
content = models.TextField(null=True, blank=True)
def __str__(self):
return self.subject_name
def get_obj_url(self):
return '/Subject/%s' %(self.subject_name)
class Subject_subsections(models.Model):
subject = models.ForeignKey(Subject, on_delete=models.CASCADE, blank = True, null=True)
course = models.ForeignKey(Course,on_delete=models.CASCADE, blank = True, null=True)
subsection_name = models.CharField(max_length=100, null=True, blank=True)
content = models.TextField(blank=True, null=True)
def __str__(self):
return self.subsection_name
class Chapter(models.Model):
subject_subsections = models.ForeignKey(Subject_subsections,on_delete=models.CASCADE, blank = True, null=True)
subject = models.ForeignKey(Subject, on_delete=models.CASCADE, blank = True, null=True)
course = models.ForeignKey(Course,on_delete=models.CASCADE, blank = True, null=True)
chapter_name = models.CharField(max_length=100, blank=True, null=True)
content = models.TextField(blank=True, null=True)
def __str__(self):
return self.chapter_name
class Topic(models.Model):
course = models.ForeignKey(Course,on_delete=models.CASCADE, blank = True, null=True)
subject = models.ForeignKey(Subject, on_delete=models.CASCADE, blank = True, null=True)
subject_subsections = models.ForeignKey(Subject_subsections,on_delete=models.CASCADE, blank = True, null=True)
chapter =
models.ForeignKey(Chapter,on_delete=models.CASCADE, blank = True, null=True)
topic_name = models.TextField(null=True, blank=True)
content = models.TextField(null=True, blank=True)
topic_image = models.ImageField(upload_to= 'topic_image', null=True, blank=True)
此函数用于对所有模型的模型使用course_content函数
def index(request):
obj = Course.objects.all()
return render(request, 'myapp/home.html', {'obj': obj})`enter code here`
def course_content(request, course_id):
course = Course.objects.get(pk=course_id)
return render(request, 'myapp/detail.html', {'course':course})
我想要该课程的科目名称及其子部分的名称,我在代码中提到了该名称
<html>
<head>
<title>content</title>
<link rel="stylesheet" type="text/css"
href="/static/css/bootstrap.min.css" >
</head>
<body>
{% block content %}
<h4 style="padding-top: 10;" ><a href="">Home</a> >> Study Material</h4>
<div>
<p style="padding-top: 20;">
<h2>{{ course.course_name }} Study Material </h2>
<hr>
</p>
</div>
<div class="container" style="padding-top: 100;">
<table class="table">
<thead>
<tr>
<th scope="col" style="text-align: center;"><h3>Table of Content</h3></th>
</tr>
</thead>
<tbody>
{% for subject in course.subject_set.all %}
<tr>
<th scope="row"><ol><a href="#">{{subject.subject_name}}</a></ol></th>
{% endfor %}
{% endblock %}
</tr>
</tbody>
</table>
</div>
<hr>
<p style="margin-left:50px; padding-top: 100;">{{ course.content }}</p>
<div style="padding-top: 200;">
<table class="table">
<thead>
<tr>
<th scope="col" style="text-align: center;"><h3>*Subject_name*</h3></th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row" >*subject-subsections*</th>
</tr>
</tbody>
</table>
</div>
</body>
</html>
enter code here
from django.conf.urls import url
from myapp import views
app_name = 'myapp'
urlpatterns = [
url(r'^course/$', views.index, name ="index"),
url(r'^course/(?P<course_id>[0-9]+)/',
views.course_content,名称=“ course_content”), ]