首页 > 解决方案 > 返回多个模板

问题描述

我目前正在尝试通过 POST 请求打开多个页面。基本上,我正在做的是通过发送 POST 请求在数据库中搜索工作编号,然后(尝试)打开与该工作编号相对应的多个页面(例如:工作位置的网页,材料的网页等.) 但是,我只能返回一个模板供浏览器打开。我看过很多很多这样的问题:

在 Flask 中一次渲染多个模板

但是,我没有找到我的问题的答案(很可能是因为我问错了问题......)无论如何,到目前为止,我的代码是这样的:

HTML:

<form action="/testing" method="POST" id="existBidFormOne" name="existBidFormOne" autocomplete="off">
        <!-- Row one holds Title-->
        <div class="center row" style="width: 100%;">
            <h1>Enter a Job Number or a Project Name:</h1>
        </div>

        <!-- Row two holds job number-->
        <div class="row">
            <!-- Column one holds Job number-->
            <div class="col-m-6">
                <div class="row centerInput">
                    <div class="col-m-4 inputPad"><b>Job Number:</b></div>
                    <div class="col-m-8 noPad">
                        <input type="text" id="exb_jobNumber" name="exb_jobNumber" class="input maxWidth" />
                    </div>
                </div>
            </div>
        </div>
        <div class="centerInput row">
            <span>
                <button type="submit" id="exb_searchOne" class="srchBtn">Search</button>
            </span>
        </div>
    </form>

烧瓶:

@app.route('/testing', methods=["GET", "POST"])
def testing():
    if request.method == 'POST':
        job_num = request.form.getlist('exb_jobNumber')
        if job_num:
            session = Session()
            results = ds.get_job_info(session, job_num) # returns dict
            results2 = ds.get_job_material(session, job_num) #returns dict
            session.close()
            # open page one
            return render_template('pageOne.html', **results)

上面的代码打开一页效果很好,但是我也想打开:

render_template('pageTwo.html', **results2)

谢谢!

标签: javascriptflask

解决方案


opening the multiple pages If you want to open multiple pages on a single webpage (i.e. in a single browser tab), then you can combine and render as many separate templates as you want with template inheritance. You can have single base page and nest many sub-pages with any content you want.

If you want to open multiple pages in multiple browser tabs, then you should return only a single main HTML page (i.e. single template), but set variable in that webpage with links of all pages you want to open in new browser tabs. Then, with the help of JavaScript and browser API method window.open() you can open those pages.


推荐阅读