Flask - 在等待任务完成时使用javascript显示加载页面

时间:2018-02-01 00:45:55

标签: javascript jquery flask jinja2

pasta.html中,点击给定链接会以这种方式呈现所选页面 <ul> <li><a href="{{ url_for('pasta') }}";>Pasta</a> <form action="{{ url_for('handle_menu') }}" method="post"> <input type="radio" name="additive" value="Cheese"> <label style="font-size: 11px;">Cheese</label> </form> </li>

@app.route('/pasta')

但是,让我们说 @app.route('/pasta') def pasta(): # (perform some long task here) return render_template('pasta.html') 必须“做饭”&#39;一些&#39;成分&#39;在它完成之前&#39;:

/cooking

就此而言,我希望在页面加载时显示@app.route('/cooking') def cooking(): return render_template('cooking.html') 视图,如下所示:

Flask

如果我没有弄错,使用jinja2200模板,您可以返回(301)或重定向(loading.html)一个页面,也可以像javascript这样的中间页面,您需要javascript

那么,我如何在此处使用cooking.html以便在等待/pasta任务完成时显示$y < 100页面,并且一旦任务完成,最后还是回来了吗?

2 个答案:

答案 0 :(得分:2)

由于您正在使用 GET 请求,因此您可以将锚标记路由到cooking,呈现cooking.html模板,然后使用{{1} }重定向到window.location.replace()pasta中的长任务将在呈现pasta()之前执行。

pasta.html

menu.html

<a href="{{ url_for('cooking') }}">Pasta</a>

cooking.html

如果是 POST 请求,您需要将表单值发送到<script> window.location.replace('/pasta'); </script> ,将其传递给模板,然后再次将其发送到cooking AJAX,最后在回调函数中重定向。

pasta

menu.html

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

cooking()

@app.route('/cooking') def cooking(): return render_template('cooking.html', form_data=request.form['form_data'])

pasta()

@app.route('/pasta', methods=['GET', 'POST']) def pasta(): if request.method == 'GET': render_template('pasta.html') # (perform some long task here) return make_response('POST request successful', 200)

cooking.html

答案 1 :(得分:1)

您需要创建名为 cooking_request 的第三个端点:

  1. import javax.persistence.Entity; import javax.persistence.GeneratedValue; @Entity public class PaideUser { @javax.persistence.Id @GeneratedValue private Integer Id; private String firstName; private String middleName; private String lastName; private String email; private String address; private String zipCode; private int phone; protected PaideUser() { } public PaideUser(Integer Id, String firstName, String middleName,String lastName, String email, String address, String zipCode, int phone) { this.Id=Id; this.firstName=firstName; this.middleName=middleName; this.lastName=lastName; this.email=email; this.address=address; this.zipCode=zipCode; this.phone=phone; } public Integer getId() { return Id; } public void setId(Integer id) { Id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getMiddleName() { return middleName; } public void setMiddleName(String middleName) { this.middleName = middleName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getZipCode() { return zipCode; } public void setZipCode(String zipCode) { this.zipCode = zipCode; } public int getPhone() { return phone; } public void setPhone(int phone) { this.phone = phone; } }
  2. /cooking_request
  3. /cooking
  4. 渲染/pasta页面并在其中使用javascript使用Ajax调用/cooking_request页面。当/cooking回复时,您将会请求/cooking内容并显示给用户。

    以下是您可以在/pasta页面中使用的示例代码。它将调用/cooking_request页面,等待它,最后在div#pasta-results元素中呈现/cooking

    /pasta