将标签值传递给views.py django

时间:2019-12-21 06:29:01

标签: python django

在我问这个问题之前,我已经在网上搜索并找到了类似的解决方案。但是,它不适用于我的情况。

我想将模板值传递给views.py。我尝试使用request.POST.get('value'),但它不起作用。我希望单击“提交”按钮时将所选学生的姓名保存在数据库中。

我收到错误消息:无法分配“''”:“ MarkAtt.studName”必须是“ Namelist”实例。

这是我的用户界面:

enter image description here

这是我的代码:

Views.py

 def AbsentStudent(request, id=None):
    classGrp = None
    attendance= 0
    today = datetime.datetime.now()
    group = GroupInfo.objects.get(id=id)
    q1 = Namelist.objects.filter(classGrp=id).values('name','id') #get the student name of the specific group
    q2 = MarkAtt.objects.all().values('studName__name','id')   #get the name of attended students fr the lab
    q3 = q1.difference(q2) #to get the absentee names: name and id only 

    form_class = studStatus
    form = form_class(request.POST)

    if request.method == 'POST':
        if form.is_valid():
            a = form.save(commit=False)
            a.currentDate = datetime.datetime.now().date()
            a.classGrp = group
            a.attendance = attendance
            a.studName = request.POST.get('studID','') //error...


            #getId = request.POST.get('id')
            #tried.studName = getId.get(id=getId)
            form.save()
            return redirect('editStudStatus',id)
        else:
            form = studStatus(request.POST )



    context = { 
    'q3' :  q3,
    'group' : group,
    'form' : form,
    'today' :today,
    # 'q5' : q5



    }

    return render(request,'studDetails.html',context, {'form':form})

模板:

   <tbody>
          {% for q in q3 %}
            <tr>


                <form method="post" enctype="multipart/form-data">
                {% csrf_token %}
<td>{{q.name}}</td> 
                <td><input type="label" value={{q.id}} name="studID"/></td></td>
                <td>{{form.status }}</td>
                <td>{{form.remarks}}</td>


                <td><button type ="submit" class="btn btn-outline-success" >Submit</button>
                </form></td>

            </tr>   

        {% endfor %}

    </tbody>

1 个答案:

答案 0 :(得分:1)

没有不同的操作就无法使用多种形式。您可以将ID传递给以下操作:

<form method="post" enctype="multipart/form-data" action="{% url "url name" id=q.id %}">