Python金字塔使用session将表单数据传递到另一个pag

2024-04-18 23:40:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是Python金字塔和Jinja2模板。我想将表单数据保存到会话中,并在另一个HTML页面中检索它。如何更改才能传递数据?我只知道如何存储我输入的数据视图.py像这样进入会话request.session['postal'] = 01934,但这不是我输入的数据交货.jinja2. 如果我使用print (session['postal']),这将只显示在我的命令提示符中,而不会显示在HTML页面中。有人能帮我吗?我是个初学者。在

在我的Views.py?在

我的HTML:交货.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>

在视图.py在

^{pr2}$

我希望之前输入的数据显示在这个确认页面上:确认.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>

Tags: namedivformidforaddresstypegroup
1条回答
网友
1楼 · 发布于 2024-04-18 23:40:57

我想,你可以把帖子从初始表单传递到确认页面的模板上,不需要会话。在

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

<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')

相关问题 更多 >