如何使一些字段在烧瓶中的形式可选

时间:2018-04-03 07:16:02

标签: python python-3.x forms flask postman

我需要知道如何处理表单中的可选字段。这是我函数的完整代码......................... .................................................. .................................................. ................

    def post(self):
    print(request.headers['Content-Type'])
    email = request.form.get('email')
    title = request.form.get('title')
    os_type=request.form.get('os_type')
    device_type=request.form.get('device_type')
    message=request.form.get('message')
    file = request.files.get('file')
    target_url = request.form.get('target_url')
    print(email)
    if email == None and email == '':
        st="email is required"
        data=error_call(st)
        return(data),400,{'Content-Type': 'application/json'}
    else:
        return (email)
        if title== None and title == '':
            st="title is required"
            data=error_call(st)
            return(data),400,{'Content-Type': 'application/json'}
        else:
            if message==None and message== "":
                return(error_call("message is requied")),400,{'Content-Type': 'application/json'}
            else:
                date=datetime.now()
                if file:
                    a=20
                    print(a)
                    if a>20000:
                        return(error_call("sixe of file exceeded"))
                    else:

                        if file.filename == '':
                            return "no file"
                        if file and allowed_file(file.filename):
                            file.filename = title
                            filename = secure_filename(file.filename)
                            file.save(os.path.join('images/push_uploads', filename))
                            image_url='http://127.0.0.1:5000/'+'images/push_uploads'+'/'+filename

                            data_message = {
                            "to":"dkmdi",
                            "content_available":"true",
                            "mutable_content":"true",
                            "data":{
                           "mediaUrl":image_url
                            },
                            "notification":{
                                "body" : "mmsiadk",
                                "type" :"message_type",
                                "sound" :"default"

                            }

                            }
                            print(image_url)
                            registration_id ="cVFt1vC16j8:APA91bEhRS1mvPn3NZGODC4zoGjcsfmhV59cpqRd_i0Rmh1fwjgIj8bdxPlLoxlJkS5HBtEeFuE-ZSt3Uc6S0RwkT9UDqTdwYOiJa0gByNc09hZT1iVFSMM2BuuEXoLg5cARmOe73S5c"
                            message_body = title
                            #result = push_service.notify_single_device(registration_id=registration_id, message_body=message_body,data_message=data_message)
                            #print(result)

当我没有使用邮递员发送一些字段时,我收到了错误

"message": "The browser (or proxy) sent a request that this server could not understand."}

所以我不需要如何处理python格式的可选字段

1 个答案:

答案 0 :(得分:4)

以此为例:

title = request.form.get('title')

如果"标题"这不会引发异常。字段未提供。