首页 > 解决方案 > Django 插入数据(获取正确数据)

问题描述

我的 html 中有这段代码

<form method="post" id="DogForm" action="/studentbehavior/"  class="myform" style="width: 100%" enctype="multipart/form-data">{% csrf_token %}
    <table class="tblcore">
    <input type="hidden" value="{{teacher}}" name="teacher">
        <tr>
            <td rowspan="2" colspan="2">Core Values</td>
            {% for core in corevalues %}
                <td colspan="8"><input type="hidden" value="{{core.id}}" name="core">{{core.Description}}</td>
            {% endfor %}
        </tr>
        <tr>
            {% for corevalues in corevaluesperiod %}
                <td colspan="4" style="font-size: 12px"><input type="hidden" value="{{corevalues.id}}" name="coredescription">{{corevalues.Description}}</td>
            {% endfor %}
        </tr>
        <tr>
            <td colspan="2">Student's Name</td>
            {% for corevalues in period %}
            <td colspan="4">
                <input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
            </td>
                        <td colspan="4">
                <input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
            </td>
                        <td colspan="4">
                <input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
            </td>
                        <td colspan="4">
                <input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
            </td>
                        <td colspan="4">
                <input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
            </td>
                        <td colspan="4">
                <input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
            </td>
                        <td colspan="4">
                <input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
            </td>
        {% endfor %}
        </tr>
        {% for students in student %}
        <tr>
            <td colspan="2" class="names"><input type="hidden" value="{{students.id}}" name="student">{{students.Students_Enrollment_Records.Students_Enrollment_Records.Students_Enrollment_Records.Student_Users}}</td>
            <td colspan="4">
                <select name="marking">
                    <option>--------</option>
                    {% for m in marking %}
                        <option value="{{m.Marking}}" >{{m.Marking}}</option>
                    {% endfor %}
                </select>
            </td>
            <td colspan="4">
                <select  name="marking">
                    <option  >--------</option>
                    {% for m in marking %}
                        <option value="{{m.Marking}}">{{m.Marking}}</option>
                    {% endfor %}
                </select>
            </td>
            <td colspan="4">
                <select  name="marking">
                    <option>--------</option>
                    {% for m in marking %}
                        <option value="{{m.Marking}}">{{m.Marking}}</option>
                    {% endfor %}
                </select>
            </td>
            <td colspan="4">
                <select  name="marking">
                    <option>--------</option>
                    {% for m in marking %}
                        <option value="{{m.Marking}}">{{m.Marking}}</option>
                    {% endfor %}
                </select>
            </td>
            <td colspan="4">
                <select name="marking">
                    <option>--------</option>
                    {% for m in marking %}
                        <option value="{{m.Marking}}">{{m.Marking}}</option>
                    {% endfor %}
                </select>
            </td>
            <td colspan="4">
                <select name="marking">
                    <option>--------</option>
                    {% for m in marking %}
                        <option value="{{m.Marking}}">{{m.Marking}}</option>
                    {% endfor %}
                </select>
            </td>
            <td colspan="4">
                <select name="marking">
                    <option>--------</option>
                    {% for m in marking %}
                        <option value="{{m.Marking}}">{{m.Marking}}</option>
                    {% endfor %}
                </select>
            </td>
        </tr>
        {% endfor %}
    </table>
    <input type="submit" value="Insert" id="submit">
</form>

这就是它在网络视图中的样子

在此处输入图像描述

这是我在视图中插入数据的代码

def studentbehavior(request):
    id = request.POST.get('teacher')
    teacher = EmployeeUser(id=id)

    coreid = request.POST.get('core')
    core = CoreValues(id=coreid)

    corevalues = []
    for corevaluesid in request.POST.getlist('coredescription'):
        corevalues.append(corevaluesid)



    coreperiodID = request.POST.get('coreperiod')
    coreperiod = gradingPeriod(id = coreperiodID)

    marking = []
    for markingID in request.POST.getlist('marking'):
        marking.append(markingID)
        print(marking.append(markingID))
    for m in request.POST.getlist('marking'):
        for student in request.POST.getlist('student'):
            students = StudentPeriodSummary(id=student)
        #s = marking[m]
            for desc in request.POST.getlist('coredescription'):
                coredescription = CoreValuesDescription(id=desc)
                V_insert_data = StudentsCoreValuesDescription(
                    Teacher=teacher,
                    Core_Values=coredescription,
                    Marking=m,
                    Students_Enrollment_Records=students,
                    grading_Period=coreperiod,
                )
                V_insert_data.save()
        return render(request, "Homepage/updatebehavior.html")

这是我的问题,即使我在选择框中仅选择一个(在管理视图中),所有标记都保存到数据花瓶中,看起来输入数据库的数据不正确。

在此处输入图像描述

这是我渴望的答案

在此处输入图像描述

更新

当我尝试 mr@Saisiva A 的答案时

for m, student, desc in zip(request.POST.getlist('marking'), request.POST.getlist('student'),
                             request.POST.getlist('coredescription')):
    coredescription = CoreValuesDescription(id=desc)
    students = StudentPeriodSummary(id=student)
    V_insert_data = StudentsCoreValuesDescription(
        Teacher=teacher,
        Core_Values=coredescription,
        Marking=m,
        Students_Enrollment_Records=students,
        grading_Period=coreperiod,
    )
    V_insert_data.save()

这是我输入的数据

在此处输入图像描述

这是我在管理站点中收到的结果

在此处输入图像描述

更新

我正在为每个学生和每个核心价值选择一个标记,然后将所有数据发送回服务器

如果我想这样输入

在此处输入图像描述

插入我的数据库的结果是这样的

在此处输入图像描述

当我尝试将此添加到我的视图中时再次更新

for markingID in request.POST.getlist('marking'):
    marking.append(markingID)


print(marking[m])

错误说

列表索引必须是整数或切片,而不是 str

标签: django

解决方案


只需修改以下格式的代码

for m, students, desc in zip(request.Post.getlist('marking'), request.Post.getlist('student'), request.Post.getlist('coredescription')):
     V_insert_data = StudentsCoreValuesDescription(
                    Teacher=teacher,
                    Core_Values=coredescription,
                    Marking=m,
                    Students_Enrollment_Records=students,
                    grading_Period=coreperiod,
                )
                V_insert_data.save()
        return render(request, "Homepage/updatebehavior.html")

推荐阅读