session.get('name')不返回任何内容,不返回表格数据

时间:2018-10-05 22:50:03

标签: python html post flask

我已经通过多个帖子对此问题进行了很多研究,并且无论我尝试什么,我都会遇到问题。从本质上讲,我正在尝试在输入字段中写一些东西,按下提交按钮,然后从输入字段中发布字符串,并在使用flask时将其显示在单独的页面/单独的路径上。我认为下面的内容是正确的,但是它返回的值是None,而不是我在index.html的输入字段中写的值。

from flask import Flask, render_template, request, jsonify, Response, 
redirect, url_for,session
from flask_bootstrap import Bootstrap
app = Flask(__name__)
Bootstrap(app)

app.secret_key = 'dljsaklqk24e21cjn!Ew@@dsa5'

@app.route('/', methods=['GET', 'POST'])

def hello():

if request.method == 'POST':
   nme = request.form['name']
   session['name'] = nme

   return url_for(deliver)

return render_template('index.html')

@app.route('/delivery', methods=['GET', 'POST'])

def deliver():
name = session.get('name')
return render_template('delivery.html', name=name)

index.html是

   <form action = "{{ url_for('deliver')}}" method = "POST">


     <p>Name <input type = text class="form-control" name = "name" /></p>
 <p>Address <input type = text class="form-control" name = "Address" /></p>


<input type=submit name='submit'>

</form>

and delivery.html是

<div class="card-deck mx-auto" style="width: 75rem;">

<div class="card text-white bg-dark p-3" style="width: 45rem;">  
 <h5 class="card-title text-center"></h5>
<img class="card-img-top mx-auto" src="/static/img/hands.png" alt="Vibration 
Image" style="width:20%">
<div class="card-body">
<h2 class="card-title text-center">Delivery Information</h2>

<h5> {{name}} </h5>

1 个答案:

答案 0 :(得分:1)

此代码:

binaries

表示提交表单将过帐到<form action = "{{ url_for('deliver')}}" method = "POST"> ,但是您确实要过帐到索引页面以触发/deliver代码。因此,只需删除操作即可。

您还需要更改

session['name'] = nme

return url_for(deliver)