如何解决“ jinja2.exceptions.UndefinedError:'zip'未定义”

时间:2018-11-23 22:13:24

标签: python flask

我正在开发一个数学应用程序,该应用程序会从用户那里获取一些输入,并使用flask来输出数学关系表,并且会发生此错误: jinja2.exceptions.UndefinedError:'zip'未定义

File "C:\Python37\Scripts\venv\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python37\Scripts\venv\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "C:\Python37\Scripts\venv\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python37\Scripts\venv\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Python37\Scripts\venv\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python37\Scripts\venv\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python37\Scripts\venv\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python37\Scripts\venv\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Python37\Scripts\venv\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python37\Scripts\venv\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\A.Toba\Desktop\Flask\simple\main.py", line 6, in index
return render_template('index.html')
File "C:\Python37\Scripts\venv\lib\site-packages\flask\templating.py", line 135, in render_template
context, ctx.app)
File "C:\Python37\Scripts\venv\lib\site-packages\flask\templating.py", line 117, in _render
rv = template.render(context)
File "C:\Python37\Scripts\venv\lib\site-packages\jinja2\asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "C:\Python37\Scripts\venv\lib\site-packages\jinja2\environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "C:\Python37\Scripts\venv\lib\site-packages\jinja2\environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python37\Scripts\venv\lib\site-packages\jinja2\_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "C:\Users\A.Toba\Desktop\Flask\simple\templates\index.html", line 55, in top-level template code
{% for i, j in zip(pwf_table,q_table) %}

main.py

在这个文件中,我试图从模板表单中获取用户输入,并将其值放入变量中,并在这些值上应用一些python代码,然后将结果发送回并发布到同一页的表中。

from flask import Flask, render_template, request
app = Flask(__name__)

@app.route('/')
def index():
   return render_template('index.html')

@app.route('/',methods = ['POST', 'GET'])
def result():
   if request.method == 'POST':
      Pr = request.form["pr"]
      Q = request.form["tq"]
      PWF = request.form["tpwf"]
      step = request.form["step"]
      pwf = list(range(0,Pr))
      q = []
      qmax = Q / (1 - 0.2*PWF/Pr - .8*(PWF/Pr)**2 )
      [q.append(qmax * (1 - 0.2*i/Pr - .8*(i/Pr)**2 )) for i in pwf ]
      pwf_table = pwf[::step]
      q_table = q[::step]
      return render_template("index.html",result = result)

if __name__ == '__main__':
   app.run(debug = True)

在此引导程序模板中,我试图从用户表单向烧瓶请求数据,并将输出发送到提交表单所生成的同一页中的表中

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link href="\static\css" rel="stylesheet">


    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <h1>Hello, world!</h1>
    <form action="http://localhost:5000" method="POST">
      <div class="form-group">
        <label for="">Pr:</label>
        <input type="text" class="form-control" name="pr" id="pwd">
      </div>
      <div class="form-group">
        <label for="pwd">Test Q:</label>
        <input type="text" class="form-control" name="tq" id="pwd">
      </div>
      <div class="form-group">
        <label for="pwd">Test PWF:</label>
        <input type="text" class="form-control" name="tpwf" id="pwd">
      </div>
      <div class="form-group">
        <label for="pwd">Table step:</label>
        <input type="text" class="form-control" name="step" id="pwd">
      </div>
      <button type="submit" class="btn btn-default">Submit</button>
   </form>


   <div class="container">
  <h2>Striped Rows</h2>
  <p>The .table-striped class adds zebra-stripes to a table:</p>
  <table class="table table-striped">
    <thead>
      <tr>
        <th>PWF</th>
        <th>Q</th>
      </tr>
    </thead>
    <tbody>
      {% for i, j in zip(pwf_table,q_table) %}
        <tr>
          <th>i</th>
          <th>j</th>
        </tr>
      {% endfor %}  
    </tbody>
  </table>
</div>

如何解决此错误

0 个答案:

没有答案