如何从模板中的多个文本字段将数据循环添加到数据库?

时间:2019-04-04 06:16:33

标签: django

我必须输入班上所有学生获得的分数。因此,我创建了一个带有文本字段的模板,用于存储与每个卷号相对应的标记。每个文本字段的ID是相应学生的卷号。

如何将这些标记从每个字段传递到views.py,以便可以将其存储在数据库中?

我尝试使用Javascript,但我不知道如何访问数据库中的表(模型中的类),以便提取每个学生的卷数并访问文本字段。

models.py

class MarksForm(forms.Form):

    exam_type = forms.CharField(label='Exam Name', widget=forms.Select(choices=EXAM_TYPES))
    entered_marks = forms.FloatField(label='Marks', required=True)

    def clean(self):
        examtype = self.cleaned_data.get("exam_type")
        enteredmarks = self.cleaned_data.get("entered_marks")
        return self.cleaned_data

views.py

    @csrf_protect
    @login_required
    def edit_marks(request, course_code):
        print ('Inside Views')
        user = request.user
        extrainfo = ExtraInfo.objects.get(user=user)

        if extrainfo.user_type == 'faculty':
            instructor = Curriculum_Instructor.objects.filter(instructor_id=extrainfo)

            for ins in instructor:
                if ins.curriculum_id.course_code == course_code:
                    registered_students = Register.objects.filter(curr_id = ins.curriculum_id.curriculum_id)

            for reg in registered_students:
                identity = Register.objects.get(student_id=reg.student_id, curr_id=curriculum[0])
                m_id = identity.r_id
                student_marks = 'enteredmarks'+identity.student_id
                score = request.POST.get(studentmarks)
                exam = request.POST.get('examtype')

                Marks.objects.create(
                    mid=m_id,
                    exam_type=exam,
                    marks=score
                    )

        context= {'registered_students': registered_students }

        return render(request, 'coursemanagement/viewperformance.html', context)

urls.py

urlpatterns = [

    url(r'^$', views.viewcourses, name='viewcourses'),
    url(r'^(?P<course_code>[A-z]+[0-9]+[A-z]?)/$', views.course, name='course'),
    url(r'^(?P<course_code>[A-z]+[0-9]+[A-z]?)/edit_marks$', views.edit_marks, name='edit_marks'),

viewperformance.html

{% load static %}
{% block viewperformance %}

<div>
  <form class="ui large form" id="entermarks" method="POST">  {% csrf_token %}

    <select name="examtype" id = "examtype" style="width: 100px">
      <option value = "1">Quiz 1</option>
      <option value = "2">Quiz 2</option>
      <option value = "3">Mid Sem</option>
      <option value = "4">End sem</option>
    </select>

    <table class="ui very basic collapsing celled table">
      <thead>
        <tr>
          <th>Students</th>
        </tr>
      </thead>

      <tbody>
        {% for x in registered_students %}
          <tr>
            <td>
              <div class="content">
                <p style="text-align:center">{{x.student_id}}</p>
                var student = x.student_id;
              </div>
            </td>
            <td>
              <input type="number" id="enteredmarks{{x.student_id}}" required="true" />
            </td>
          </tr>

        {% endfor %}
      </tbody>
    </table>
    <input type="submit" class="ui primary button" id="submit_marks" value="Upload" href="/ocms/{{course.course_id}}/edit_marks"/>

  </form>
</div>
{% endblock %}```

When I try to print something inside view.py, it does not appear. Why is html not getting linked to its corresponding view(edit_marks)?

1 个答案:

答案 0 :(得分:0)

在django中执行此操作的最佳方法是使用Django Formset

表单集是抽象层,可用于同一页面上的多个表单。这是您的要求。