Python 如何从视图创建表并将其显示为HTML?

Python 如何从视图创建表并将其显示为HTML?,python,django,python-3.x,Python,Django,Python 3.x,如何使用此查询创建相互对应的表 视图.py corevalues = CoreValues.objects.all().order_by('Display_Sequence') marking = StudentBehaviorMarking.objects.all() corevaluesdescription = CoreValuesDescription.objects.values('id', 'Description').distinct( 'Description')

如何使用此查询创建相互对应的表

视图.py

corevalues = CoreValues.objects.all().order_by('Display_Sequence')
marking = StudentBehaviorMarking.objects.all()
corevaluesdescription = CoreValuesDescription.objects.values('id', 'Description').distinct(
        'Description').order_by('Description')
period = gradingPeriod.objects.filter(id=coreperiod).order_by('Display_Sequence')
studentcorevalues = StudentsCoreValuesDescription.objects.filter(Teacher=teacher).filter(
            GradeLevel=gradelevel.values_list('Description')) \
            .values('Students_Enrollment_Records').distinct('Students_Enrollment_Records').order_by(
            'Students_Enrollment_Records')
student = StudentPeriodSummary.objects.filter(Teacher=teacher).filter(
            GradeLevel__in=gradelevel.values_list('id'))
我想要达到的结果

students = StudentsCoreValuesDescription.objects.filter(grading_Period=coreperiod) \
            .values('id', 'Marking',
                    'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Firstname',
                    'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname') \
            .distinct(
            'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Firstname',
            'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname') \
            .order_by(
            'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname',
            'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Firstname')

        gradelevel = EducationLevel.objects.filter(id__in=coregradelevel).distinct().order_by('id')
        markings = StudentBehaviorMarking.objects.all()
        corevaluesperiod = CoreValuesDescription.objects.filter(grading_Period=coreperiod).order_by('Display_Sequence')
        table = []
        student_name = None
        table_row = None
        columns = len(corevaluesperiod) + 1

        table_header = ['Core Values']
        table_header.extend(corevaluesperiod)


        table.append(table_header)
        for student in students:
            if not student[
                       'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Firstname'] + ' ' + \
                   student[
                       'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname'] == student_name:
                if not table_row is None:
                    table.append(table_row)
                table_row = [None for d in range(columns)]
                student_name = student[
                                   'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Firstname'] + ' ' + \
                               student[
                                   'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname']
                table_row[0] = student_name
                print(table_row[0])
            table_row[student.index(gradelevel['Description']) + 1] = markings['Marking']
        table.append(table_row)
        return render(request, "Homepage/behavior.html", {"table": table, "corevaluesperiod" \
            : corevaluesperiod, "corevalues": corevalues, "marking": marking})
 <tr>
        {% for v in table.0 %}
        <th id="thupdate">{{ v }}</th>
        {% endfor %}
        <th  hidden></th>
    </tr>

    <tbody>
    {% for row in table|slice:"1:" %}
        <tr class="tr2update">
            <td>{{ row.0  }}</td>
             <td class="tdupdate" hidden><input type="text" hidden></td>
            {% for c in row|slice:"1:" %}
            <td><input type="text" id="oldgrade" class="oldgrade" name="gradeupdate" value="{{c}}"></td>
            {% endfor %}
        </tr>
     {% endfor %}

我的车型

class CoreValues(models.Model):
    Description = models.CharField(max_length=500, null=True, blank=True)
    Display_Sequence = models.IntegerField(null=True, blank=True)
    .

class CoreValuesDescription(models.Model):
    Core_Values = models.ForeignKey(CoreValues,on_delete=models.CASCADE, null=True)
    Description = models.TextField(max_length=500, null=True, blank=True)
    grading_Period = models.ForeignKey(gradingPeriod, on_delete=models.CASCADE)
    Display_Sequence = models.IntegerField(null=True, blank=True)

class StudentBehaviorMarking(models.Model):
    Marking = models.CharField(max_length=500, null=True, blank=True)
    Non_numerical_Rating = models.CharField(max_length=500, null=True, blank=True)

class StudentPeriodSummary(models.Model):
    Teacher = models.ForeignKey(EmployeeUser, on_delete=models.CASCADE,null=True, blank=True)
    Students_Enrollment_Records = models.ForeignKey(StudentSubjectGrade,on_delete=models.CASCADE)
可以使用Python创建一个表,对吗

更新

当我尝试将其添加到我的视图.py时:

students = StudentsCoreValuesDescription.objects.filter(grading_Period=coreperiod) \
            .values('id', 'Marking',
                    'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Firstname',
                    'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname') \
            .distinct(
            'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Firstname',
            'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname') \
            .order_by(
            'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname',
            'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Firstname')

        gradelevel = EducationLevel.objects.filter(id__in=coregradelevel).distinct().order_by('id')
        markings = StudentBehaviorMarking.objects.all()
        corevaluesperiod = CoreValuesDescription.objects.filter(grading_Period=coreperiod).order_by('Display_Sequence')
        table = []
        student_name = None
        table_row = None
        columns = len(corevaluesperiod) + 1

        table_header = ['Core Values']
        table_header.extend(corevaluesperiod)


        table.append(table_header)
        for student in students:
            if not student[
                       'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Firstname'] + ' ' + \
                   student[
                       'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname'] == student_name:
                if not table_row is None:
                    table.append(table_row)
                table_row = [None for d in range(columns)]
                student_name = student[
                                   'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Firstname'] + ' ' + \
                               student[
                                   'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname']
                table_row[0] = student_name
                print(table_row[0])
            table_row[student.index(gradelevel['Description']) + 1] = markings['Marking']
        table.append(table_row)
        return render(request, "Homepage/behavior.html", {"table": table, "corevaluesperiod" \
            : corevaluesperiod, "corevalues": corevalues, "marking": marking})
 <tr>
        {% for v in table.0 %}
        <th id="thupdate">{{ v }}</th>
        {% endfor %}
        <th  hidden></th>
    </tr>

    <tbody>
    {% for row in table|slice:"1:" %}
        <tr class="tr2update">
            <td>{{ row.0  }}</td>
             <td class="tdupdate" hidden><input type="text" hidden></td>
            {% for c in row|slice:"1:" %}
            <td><input type="text" id="oldgrade" class="oldgrade" name="gradeupdate" value="{{c}}"></td>
            {% endfor %}
        </tr>
     {% endfor %}
这就是我如何将数据发布到我的html

students = StudentsCoreValuesDescription.objects.filter(grading_Period=coreperiod) \
            .values('id', 'Marking',
                    'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Firstname',
                    'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname') \
            .distinct(
            'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Firstname',
            'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname') \
            .order_by(
            'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname',
            'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Firstname')

        gradelevel = EducationLevel.objects.filter(id__in=coregradelevel).distinct().order_by('id')
        markings = StudentBehaviorMarking.objects.all()
        corevaluesperiod = CoreValuesDescription.objects.filter(grading_Period=coreperiod).order_by('Display_Sequence')
        table = []
        student_name = None
        table_row = None
        columns = len(corevaluesperiod) + 1

        table_header = ['Core Values']
        table_header.extend(corevaluesperiod)


        table.append(table_header)
        for student in students:
            if not student[
                       'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Firstname'] + ' ' + \
                   student[
                       'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname'] == student_name:
                if not table_row is None:
                    table.append(table_row)
                table_row = [None for d in range(columns)]
                student_name = student[
                                   'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Firstname'] + ' ' + \
                               student[
                                   'Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname']
                table_row[0] = student_name
                print(table_row[0])
            table_row[student.index(gradelevel['Description']) + 1] = markings['Marking']
        table.append(table_row)
        return render(request, "Homepage/behavior.html", {"table": table, "corevaluesperiod" \
            : corevaluesperiod, "corevalues": corevalues, "marking": marking})
 <tr>
        {% for v in table.0 %}
        <th id="thupdate">{{ v }}</th>
        {% endfor %}
        <th  hidden></th>
    </tr>

    <tbody>
    {% for row in table|slice:"1:" %}
        <tr class="tr2update">
            <td>{{ row.0  }}</td>
             <td class="tdupdate" hidden><input type="text" hidden></td>
            {% for c in row|slice:"1:" %}
            <td><input type="text" id="oldgrade" class="oldgrade" name="gradeupdate" value="{{c}}"></td>
            {% endfor %}
        </tr>
     {% endfor %}

{表中v的%0%}
{{v}
{%endfor%}
{%用于表|切片:“1:%”中的行
{{row.0}}
{第|行中c的%slice:“1:%”
{%endfor%}
{%endfor%}
这是我的网页中的结果:


您看过django表格模块了吗?:。我可以控制那里的标题和列吗?虽然使用了许多模型,但django表格是否具有对第一行和左上角单元格中所暗示的单元格跨度的精细控制水平?这就是答案?或者我必须通过视图创建表?您好。看起来你需要做的一部分是CSS。e、 g.您的
td
s需要边框。首先,可以尝试更新
tdupdate
类,使其包含
边框:1px纯色
。另外,您在标题的循环中有
,但我相信您需要
class=“thupdate”
<代码>id在页面上是唯一的。