小编Min*_*inh的帖子

如何正确使用 get_context_data 和 ListView 来获取 Django 中的相关实例

我正在尝试使用 ListView 和 ContextMixin 创建视图,但我不确定这是否是正确的方法。目标是获取 中的所有字段CourseImplementation以及teacherid与其连接的TeacherCourseImplementation。问题是在浏览器上打开模板时
我看不到该部分。implement.teacherid我不知道如果我想拿到教师证该怎么办。

class ImplementView(generic.ListView):
    template_name = 'schedule/implement.html'
    context_object_name = 'all_implements'

    def get_queryset(self):
        return CourseImplementation.objects.all()

    def get_context_data(self, **kwargs):
        context = super(ImplementView, self).get_context_data(**kwargs)
        context['teacherid'] = TeacherCourseImplementation.objects.all()
        return context
Run Code Online (Sandbox Code Playgroud)

这是我的 models.py

class CourseImplementation(models.Model):
    courseid = models.ForeignKey(Course, on_delete=models.CASCADE, db_column='courseid', )
    roomid = models.ForeignKey('Room', models.DO_NOTHING, db_column='roomid', blank=True, null=True)
    note = models.CharField(max_length=10, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'course_implementation'

    def __str__(self):
        return self.pk + ' - ' + self.courseid

class …
Run Code Online (Sandbox Code Playgroud)

python django django-views

0
推荐指数
1
解决办法
2126
查看次数

标签 统计

django ×1

django-views ×1

python ×1