将数据从两个模型传递到一个HTML页面-Django [英] Pass data from two models into one single html page - Django
问题描述
我面临将两个模型中的数据显示到单个html页面中的问题
i'm facing problem with show data from two models into single html page
我有两个模型-第一个称为课程
第二个叫课程
好的,所以我想添加课程,然后在课程中添加课程,并在一个html页面中显示数据
i have two models - first called Course
and second called Lesson
okay , so i want add course then add lesson in the course and show data in one html page
模型。 py:
class Course(models.Model):
title = models.CharField(max_length=100,default="")
name = models.CharField(max_length=100,default="")
app_contect = RichTextField(blank=True,null=True)
app_image = models.ImageField(upload_to='images/',null=True, blank=True)
post_date = models.DateTimeField(auto_now_add=True, null=True, blank=True)
post_tag = models.CharField(max_length=50,default="",choices = COURSE_SECTION)
objects = SearchManager()
slug = models.SlugField(default="",blank=True,unique=True,editable=True)
def save(self, *args, **kwargs):
if not self.id or not self.slug:
super(Course, self).save(*args, **kwargs)
self.slug = slugify(f"{self.title} {str(self.id)}")
super(Course, self).save(*args, **kwargs)
def get_image(self):
if self.app_image and hasattr(self.app_image, 'url'):
return self.app_image.url
else:
return '/path/to/default/image'
def __str__(self):
return self.name
class Meta:
ordering = ('-post_date',)
class Lesson(models.Model):
course = models.ForeignKey(Course, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
youtube_url = models.URLField()
def __str__(self):
return self.name
admin.py:
from django.contrib import admin
from blog_app.models import Course,Lesson
class InlineLessons(admin.StackedInline):
model = Lesson
class CourseAdmin(admin.ModelAdmin):
inlines = [ InlineLessons ]
admin.site.register(Lesson)
admin.site.register(Course,CourseAdmin)
如何显示课程数据和将课程模型放到一个html页面中?
so how to show data from course and lesson model into one html page ?
i tried do this but it's don't work , it just show data from Course model
views.py:
def course_posts(request,slug):
lesson = Lesson.objects.all()
course_posts = get_object_or_404(Course, slug=slug)
context = {'course_posts':course_posts}
return render(request,'course/course_posts_page.html', { 'course_posts': course_posts,'lesson':lesson })
html页面:
<h1 id="font_control_for_header_in_all_pages"> {{course_posts.name}} </h1>
<div id="accordion">
<div class="card">
<div class="card-header" id="headingOne">
<h5 class="mb-0 text-center">
<button class="btn btn-link" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
{{lesson.name}}
</button>
</h5>
</div>
<div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordion">
<div class="card-body">
<iframe width="100%" height="400px" src="{{lesson.youtube_url}}" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</div>
</div>
</div>
这不起作用,它显示的是 Course
模型中的数据并非来自课程和课程
this don't work , it's show data from Course
model not from lesson and course
推荐答案
您似乎想显示与课程相关的所有课程,因此您应该修改自己的课程
It looks like you want to display all the lessons associated with a course, so you should modify your view to not made the lesson query.
def course_posts(request,slug):
course_posts = get_object_or_404(Course, slug=slug)
context = {'course_posts':course_posts}
return render(request,'course/course_posts_page.html', { 'course_posts': course_posts })
<h1 id="font_control_for_header_in_all_pages"> {{course_posts.name}} </h1>
<div id="accordion">
{% for lesson in course_posts.lesson_set.all %}
<div class="card">
<div class="card-header" id="headingOne">
<h5 class="mb-0 text-center">
<button class="btn btn-link" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
{{lesson.name}}
</button>
</h5>
</div>
<div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordion">
<div class="card-body">
<iframe width="100%" height="400px" src="{{lesson.youtube_url}}" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</div>
</div>
{% endfor %}
</div>
这篇关于将数据从两个模型传递到一个HTML页面-Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!