简体   繁体   中英

bottle request.forms.get returns None

I am trying to send information from a web form into python using bottle. I am using get to send a variable "test" but when i call the request function for "test", it returns none. I've been using the bottle tutorial as my reference.

Here is my web form:

<form action="http://localhost:8080/page" method="get">
<input type="number" name="test" step="5">
<input type="submit" name="my-form" value="GO">
</form>

If you type 1 in the field and click go, this brings you to the following URL:

http://localhost:8080/page?test=10&my-form=GO

And here is the bottle python code:

@route('/page', method='GET')
def index():
testvar = request.forms.get('test')
return 'Hello %s' % testvar

From what I understand, request.forms.get('test') should retrieve the value from test=10 in the url and pass it into testvar. However, I receive a value of none, meaning the var is empty.

Thanks!

According to the documentation , request.forms will only collect the POST and PUT parameters:

forms

Form values parsed from an url-encoded or multipart/form-data encoded POST or PUT request body. The result is returned as a FormsDict. All keys and values are strings.

In your case, you have the HTTP GET form, use request.params .

Since you are doing a HTTP GET, the parameters are not passed as a form but as query parameters. Bottle provides request.query to access these parameters:

The query_string parsed into a FormsDict. These values are sometimes called “URL arguments” or “GET parameters”, but not to be confused with “URL wildcards” as they are provided by the Router.

The request.forms is to be used for a HTTP POST:

Form values parsed from an url-encoded or multipart/form-data encoded POST or PUT request body. The result is returned as a FormsDict. All keys and values are strings. File uploads are stored separately in files.

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