简体   繁体   中英

passing label value to views.py django

Before i asked this question, I have searched online and found similar solutions. However, it does not work for my case.

I would like to pass template value to views.py. I tried using request.POST.get('value') but it does not work. I would like the name of the selected student to be saved in the database when the submit button is clicked.

i received an error: Cannot assign "''": "MarkAtt.studName" must be a "Namelist" instance.

Here is my UI:

在此处输入图片说明

Here are my codes:

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})

Template:

   <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>

不可能在没有不同操作的情况下使用多个表单。您可以将您的 id 传递给操作,如下所示:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM