简体   繁体   English

request.POST.get() 返回值,无

[英]request.POST.get() returns the value and none

I have a form that submits the absentee's name to the database.我有一个将缺席者姓名提交到数据库的表格。 I tried to pass the name via request.POST.get to the views.py.我试图通过 request.POST.get 将名称传递给 views.py。 However, it passed the name and none together and it does not save to database.但是,它将 name 和 none 一起传递,并且不会保存到数据库中。

Below is my code:下面是我的代码:

views.py视图.py

def AbsentStudent(request, id=None):
        wk = LabDuration.objects.get(sDate=today)   
wk1 = MarkAtt.objects.filter(currentDate=today)

if wk.sDate == today.date() and wk1 == today.date():
    wk2 = wk1.week
    if wk2 == wk1.week:
        q1 = Namelist.objects.filter(classGrp=id).values('name')
        q2 = MarkAtt.objects.all().values('studName__name')
        q3 = q1.difference(q2)

else:
    q1 = Namelist.objects.filter(classGrp=id).values('name')
    q2 = MarkAtt.objects.all().values('studName__name')
    q3 = q1.difference(q2)
    form_class = studStatus
    form = form_class(request.POST)
    if request.method == 'POST':
        if form.is_valid():
            a = form.save(commit=False)
            a.studName__name = request.POST.get("sname") //does not submit into database

            form.save()
            return redirect('editStudStatus',id)
        else:
            form = form = studStatus(request.POST )


    context = { 
    'form' : form,
     'q3':q3
}

    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><input type="text" name="sname" values="{{q.name}}">{{q.name}}
                <td>{{form.status }}</td>
                <td>{{form.remarks}}</td>


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

            </tr>   

        {% endfor %}

    </tbody>

Saving with commit=False gets you a model object, then you can add your extra data and save it.使用commit=False保存会为您提供一个模型对象,然后您可以添加额外的数据并保存它。

So, you need to change form.save() to a.save()因此,您需要将form.save()更改为a.save()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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