django - 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
解决方案
只需修改以下格式的代码
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")
推荐阅读
- mysql - 尝试使用 Mysql.Data.Entity 时出现异常
- sitecore9 - Sitecore 9.1 安装错误
- java - JavaFX - 使用来自不同类的对象参数启动应用程序
- javascript - 在数据集中的引号内添加 JSON 结果
- java - 使用 ObjectMapper 反序列化到参数化类时出现 ClassCastException,即使使用 TypeReference 和 TypeFactory
- sql-server - 用于清理称呼的正则表达式功能
- kubernetes - Kiali 在 aws 上的 kubernetes 上安装错误 [错误:无法识别“STDIN”:版本“v1”中的种类“ClusterRole”没有匹配项]
- python - Keras 在反向传播的多个输出的多个损失中找到最佳损失
- c# - windows wpf MVVC模式,如何在该模式中适应代码逻辑
- ruby - 使用 Knife ssh 命令时,它在客户端使用的 ruby 版本与我手动 ssh 到它时不同