dev-notes/python/libs/web/flask/flask-requests.md

35 lines
818 B
Markdown
Raw Normal View History

2021-01-31 11:05:37 +01:00
# Flask Requests
Specify allowed HTTP methods in `file.py`:
`@app.route("/page/", methods=["allowed methods"])`
## Forms
in `file.py`:
```py
from flask import Flask, render_template
from flask.globals import request
@app.route("/login/", methods=["GET", "POST"])
def login():
if request.method == "POST": # if POST then form has been filled
data = request.form["field name"] # store the form's data in variable
# manipulate form data
req_args = request.args # request args
else: # if GET then is asking for form page
return render_template("login.html")
```
In `login.html`:
```html
<html>
2021-09-20 19:35:32 +02:00
<!-- action="#" goes to page itself but with # at the end of the URL -->
2021-01-31 11:05:37 +01:00
<form action="#" method="post">
<input type="text" name="field name">
</html>
```