입력양식에 데이터를 입력하면 서버는 이를 주고 받아서 사용하게끔 하는데 이때 사용하는 것이 methods 이다.
서버
라우트함수에 methods 매개변수를 추가하여 사용하는데
@app.route("/something", methods=["GET", "POST"])
def something():
if request.method =='POST':
return redirect(url_for('home'))
return render_template('something.html')
입력양식을 누르는 식의 post가 있었다면
함수 home으로 돌아가라는 somehting함수를 만들었다.
something.html
<form action="{{url_for('something')}}" method="post">
<label>당신의 별자리</label>
<input name='constellation' type="text">
<label>당신의 태어난 월</label>
<input name='month of birth' type="text">
<label>당신의 탄생화</label>
<input name='flower' type="text">
<button type="submit">제출하기!</button>
</form>
home페이지에서 작성한 것들을 <li>태그에 넣어서 띄어보자
home함수에 변수1로 리스트 이름을 넣는다.
@app.route('/')
def home():
return render_template('index.html',vra1=list1)
받은 입력을 리스트1에 저장해서 사용하기 위해 something함수에서 받은 값들은 새로운 딕셔너리에 저장한다.
@app.route("/something", methods=["GET", "POST"])
def something():
if request.method =='POST':
list_new={
'constellation':request.form['constellation'],
'month_of_birth' :request.form['month_of_birth'],
'flower' : request.form['flower']
}
list1.append(list_new)
return redirect(url_for('home'))
return render_template('something.html')
홈페이지로 재경로가 설정되어 돌아갈 때에 입력받은 데이터들을 나타내자
<div class="container">
<h1>당신의 개인 정보ㅎ</h1>
{% if lists == []: %}
<p>list is empty</p>
{% endif %}
<ul>
{% for item in lists %}
<li>당신의 별자리는 {{item.constellation}}, 그렇다면 태어난 달은 {{item.month_of_birth}}, 탄생화는 {{item.flower}}일테지요</li>
{% endfor %}
</ul>
</div>