Flask Form Post, Get
2023. 8. 7. 20:50ㆍFlask
from flask import Blueprint, render_template, request
form_test = Blueprint("form_test",__name__,url_prefix= '/form')
@form_test.route('/test',methods=('POST','GET',))
def test_form():
if request.method == "POST":
form = request.form
data = form['title']
print(data)
content = form['content']
print(content)
test = {'title':data, 'content':content}
return render_template("post_get_test/form_post.html",form = test)
else:
return render_template("post_get_test/form_get.html")
request가 GET이면 값을 입력 받아 client에게 html을 return한다
request가 POST이면 request로 부터 form을 읽어들여
이를 활용 할 수 있다
위 함수는 form data의 값들을 print하고
이 값을 보여주는 html 파일에 render하여 보낸다
<form method = "POST">
<p>제목 : <input type="text" name = "title" value = "test title"></p>
<p>내용 : <input type="text" name = "content" value = "test content"> </p>
<input type="submit">
</form>
form을 보내는 html을 기본적으로 이와 같다
이때 form method 뒤에 주소를 붙여 원하는 주소로 보낸 수도 아니면 따로 주소를 지정하지 않아
현재 주소로 보낼 수도 있다
{{form.title}}<br>
{{form.content}}
{{}}안의 값은 render_template중 값이 바뀌어 client에 return 된다
'Flask' 카테고리의 다른 글
| MongoDB (0) | 2023.12.11 |
|---|---|
| Route (0) | 2023.12.11 |
| render_template (0) | 2023.12.10 |
| Blueprint (1) | 2023.12.10 |
| Flask Studying Project (0) | 2023.08.07 |