Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从POST请求获取数据_Python_Django - Fatal编程技术网

Python 从POST请求获取数据

Python 从POST请求获取数据,python,django,Python,Django,尽管正确地更新了django模型,但POST数据并不总是符合我的逻辑 def new_record(request): form = RecordForm(request.POST or None) if request.method == 'POST': if form.is_valid(): form.save() return HttpResponseRedirect('/new_record')

尽管正确地更新了django模型,但POST数据并不总是符合我的逻辑

def new_record(request):
    form = RecordForm(request.POST or None)

    if request.method == 'POST':
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/new_record')
        else:
            form = RecordForm()

    item1 = request.POST.getlist('checkbox_1')
    item2 = request.POST.getlist('checkbox_2')
    item3 = request.POST.getlist('checkbox_3')

    print(item1)
    print(item2)
    print(item3)

    if 'on' in item1:
        print("Checkbox 1 is true")
        write_pdf_view(textobject='textobject', exam_record_number='123')
    else:
        print("Checkbox 1 is False")

    if 'on' in item2:
        print("Checkbox 2 is true")
    else:
        print("Checkbox 2 is False")

    if 'on' in item3:
        print("Checkbox 3 is true")
    else:
        print("Checkbox 3 is False")


    return render(request=request,
                  template_name='main/new_record.html',
                  context={"form": form}
                  )
我希望做的基本上是检查是否选中了复选框,如果这是真的,则将值传递到函数中,因为现在我已将y write_pdf_view值固定到我知道存在的东西上,而这也不起作用(我在上面导入了该值)


我觉得这对有经验的人来说可能是微不足道的,我是一个新的爱好者,只是想学习!非常感谢您的帮助。

您的if语句在GET而不是POST期间执行

我建议您使用基于类的视图结构构建代码,如下所示:

from django.views import View

class NewRecord(View):

    def get(self, request):
        return render(request, 'main/new_record.html', {'form': RecordForm})

    def post(self, request):
        form = RecordForm(request.POST)

        if form.is_valid():
            form.save()

        item1 = request.POST.get('checkbox_1', None)
        ##place the rest of your logic here

        return HttpResponseRedirect('/new_record')

您遇到的错误或问题是什么?你的问题不清楚很好,谢谢你的建议。一路上我学到了一些东西。现在看来,我的复选框on或none实际上正在传递到逻辑以进行进一步的处理。