Python金字塔 - 使用会话将表单数据传递到另一个页面

时间:2017-11-13 01:37:37

标签: python forms session jinja2 pyramid

我正在使用带有Jinja2模板的Python Pyramid。我想将表单数据保存到会话中并在另一个HTML页面中检索它。我应该如何更改才能传递数据?我只知道如何将Views.py中的数据密钥存储到像request.session['postal'] = 01934这样的会话中,但这不是我在Delivery.jinja2中键入的数据。如果我使用print (session['postal']),这只会在我的命令提示符中显示,而不会在HTML页面中显示。谁能帮我吗?我是这个的初学者。

在我的Views.py中添加/更改内容?

我的HTML:Delivery.jinja2

<form class="form-horizontal" method="POST">   
<div class="form-group">
    <label class="control-label col-md-2" for="postal">Postal Code:</label>
        <input type="text" class="form-control" id="postal" placeholder="Enter Postal Code" name="postal" />
</div>
<div class="form-group">
    <label class="control-label col-md-2" for="address">Detailed Address:</label>
        <textarea class="form-control" rows="3" id="address" placeholder="Enter Address" name="address"></textarea>
</div>
<div class="form-group">
    <label class="control-label col-md-2" for="unit">Unit No #:</label>
        <input type="text" class="form-control" id="unit" placeholder="Enter Unit No" name="unit" />
</div>
<button type="submit" class="btn btn-default" name="submit">Submit</button>
</form>

Views.py

@view_config(route_name='deliveryLink', renderer='templates/deliveryLink.jinja2')
def deliveryLink(request):
    print("YAY for gift delivery via Link")

if 'submit_deliverylink' in request.POST:
    print("request.POST: ", request.POST)

    myform = request.POST

    for m in myform:
        print("key: ", m, " value: ", myform[m])

    session = request.session

    session['postal'] = ?
    session['address'] = ?
    session['unit'] = ?

    data = "??"

    data_array = data.split(",")
    session['data'] = data_array

    session['delivery'] = str(data_array)

    print (session['delivery'])

    return HTTPFound(location='http://localhost:5555/confirmation')
return {}

@view_config(route_name='confirmation', renderer='templates/confirmation.jinja2')
def confirmation(request):
    print("YAY for confirmation")

        for a in request.POST:
            request.session[a] = request.POST[a]

    return {}

我希望之前输入的数据显示在此确认页面上:Confirmation.jinja2

<form class="form-horizontal" method="POST">   
    <div class="form-group">
        <label class="control-label col-md-2" for="postal">Postal Code:</label>
            <input type="text" class="form-control" id="postal"  name="postal" />
    </div>
    <div class="form-group">
        <label class="control-label col-md-2" for="address">Detailed Address:</label>
            <textarea class="form-control" rows="3" id="address" name="address"></textarea>
    </div>
    <div class="form-group">
        <label class="control-label col-md-2" for="unit">Unit No #:</label>
            <input type="text" class="form-control" id="unit" name="unit" />
    </div>
    </form>

1 个答案:

答案 0 :(得分:0)

我认为,您可以将POST从初始表单传递到确认页面的模板,无需会话。

如果您需要会话,可以从模板中调用

<input type="text" class="form-control" id="postal" name="postal" value="{{session['postal']}}" />

# after form submitted, it sends post request, just check if it exist
if request.POST:
    print("request.POST: ", request.POST)

    myform = request.POST
    # you need iterate over keys for this case
    for m in myform.keys():
        print("key: ", m, " value: ", myform[m])

    session = request.session
    # you can access request.POST directly or use your variable myfrom 
    # use myform.get('postal','') to get value by key 
    session['postal'] = myform.get('postal','')
    session['address'] = myform.get('postal','')
    session['unit'] = myform.get('unit','')

    data = "??"

    data_array = data.split(",")
    session['data'] = data_array

    session['delivery'] = str(data_array)

    print (session['delivery'])

    return HTTPFound(location='http://localhost:5555/confirmation')