如何从一种形式创建多个记录

时间:2019-03-24 18:28:17

标签: django-templates django-views

我想以一种形式在我的mysql数据库中添加多行...

这是我的模板。我正在遍历学生对象。我想一键为所有这些学生加分。

<form method="POST" action="{% url 'Automation:Automation/mark' %}"> 
{%csrf_token %}
     <table border="1px">

   <tr>
          <td><label>UserID</label></td>
       <td><label>Mark:</label></td>
   </tr>
         {% for student in student %}
         <tr>
        <td><input type="text" name="studentid" value="{{ student.UserID}}">
    </td>
         <td><input type="text" name="mark"></td>
         </tr>

    {% endfor %}
         <tr>
         <td align="center"><button type="submit" 
class="approve1">Submit</button></td>
         <td align="center"> <a href="{% url 
'Automation:Automation/addresult' %}" class="btn btn-default 
approve1">Back</a></td>
     </tr>
        </table>

</form>

这是我的观点,它肯定只添加了最后一个输入行。

def mark(request):
    exam = request.session['exam']
    subject = request.session['subject']
    if request.method == 'POST':
        mark = request.POST['mark']
        studentid = request.POST['studentid']
        post = result.objects.create(
            studentid=studentid,
            examid=exam,
            subjectid=subject,
            mark=mark,
            year=datetime.now().year,
            added_by=str(request.user)
        )
        post.save()
        return redirect('/Automation/addresult')
    return render(request, 'Automation/mark.html')

0 个答案:

没有答案