0

我开始从在我的烧瓶应用程序中使用标准基本 SQL 切换到使用 peewee,并且遇到了一个奇怪的错误,我似乎无法找到任何信息。我的端点工作正常,但是当我尝试进入登录页面时,我得到“jinja2.exceptions.UndefinedError: 'peewee.IntegerField object' has no attribute 'flags'”

这似乎与 wtforms 和 peewee 进行了一些奇怪的交互,但我似乎找不到类似的问题。提前致谢。

请注意,所有内容都在一个文件中

我的模型:

class pipelineForm(FlaskForm):
    pipeline = IntegerField('Pipeline ID')
class Process(Model):
    pipeline_id = IntegerField()
    process_name= CharField(null = True)
    log= CharField(null = True)
    exit_code= IntegerField()
    started= CharField(null = True)
    finsihed= CharField(null = True)

    class Meta:
        database=db

登陆页面的终点:

@app.route('/bloodhound', methods=['GET','POST'])
def index():
    form =pipelineForm()
    print(form.errors)
    if form.validate_on_submit():
        print(str(form.pipeline.data))
        return redirect(url_for('.display', pipelineId=form.pipeline.data))#'<h1>' + str(form.pipeline.data) + '</h1>'
    return render_template('index.html',form=form)

登陆页面:

{% extends "bootstrap/base.html" %} {%import "bootstrap/wtf.html" as wtf%} {% block content %}
<!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 -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">

    <title>Narrow Jumbotron Template for Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <link href="../../dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link href="../../assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="jumbotron-narrow.css" rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
    <script src="../../assets/js/ie-emulation-modes-warning.js"></script>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[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>

<nav class="navbar navbar-inverse">
    <div class="container-fluid">
        <div class="navbar-header">
            <a class="navbar-brand" href="/bloodhound">Bloodhound</a>
        </div>
        <ul class="nav navbar-nav">
            <li class="active"><a href="/bloodhound">Home</a></li>
            <li><a href="#">Performance</a></li>
        </ul>
    </div>
</nav>

<body>

    <div class="container">


        <div class="jumbotron">
            <h1>Welcome to Bloodhound!</h1>
            <p class="lead">Enter a pipeline Id to get diagnostic information.</p>
            <form class="input" method="POST" action="/bloodhound">
                <div class="input-group" style="width: 300px;">
                    {{form.hidden_tag()}} {{wtf.form_field(form.pipeline)}}
                    <span class="input-group-btn" style="vertical-align: bottom;">
                    <button class="btn btn-default" type="submit" >Go!</button>
                    </span>

                </div>
            </form>


        </div>



        <footer class="footer">
            <p>&copy; 2017 MITRE.</p>
        </footer>

    </div>
    <!-- /container -->


    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>
</body>

</html>
{% endblock %}

全栈跟踪

Traceback (most recent call last):
  File "/home/patrick/enviroments/venv/lib/python3.5/site-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/patrick/enviroments/venv/lib/python3.5/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/home/patrick/enviroments/venv/lib/python3.5/site-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/patrick/enviroments/venv/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/patrick/enviroments/venv/lib/python3.5/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/patrick/enviroments/venv/lib/python3.5/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/patrick/enviroments/venv/lib/python3.5/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/patrick/enviroments/venv/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/patrick/enviroments/venv/lib/python3.5/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/patrick/enviroments/venv/lib/python3.5/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/patrick/process_tracker/api/tracker_api.py", line 201, in index
    return render_template('index.html',form=form)
  File "/home/patrick/enviroments/venv/lib/python3.5/site-packages/flask/templating.py", line 134, in render_template
    context, ctx.app)
  File "/home/patrick/enviroments/venv/lib/python3.5/site-packages/flask/templating.py", line 116, in _render
    rv = template.render(context)
  File "/home/patrick/enviroments/venv/lib/python3.5/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/home/patrick/enviroments/venv/lib/python3.5/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/patrick/enviroments/venv/lib/python3.5/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
 File "/home/patrick/process_tracker/api/templates/index.html", line 1, in top-level template code
    {% extends "bootstrap/base.html" %} {%import "bootstrap/wtf.html" as wtf%} {% block content %}
  File "/home/patrick/enviroments/venv/lib/python3.5/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 1, in top-level template
 code
    {% block doc -%}
  File "/home/patrick/enviroments/venv/lib/python3.5/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 4, in block "doc"
    {%- block html %}
  File "/home/patrick/enviroments/venv/lib/python3.5/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 20, in block "html"
    {% block body -%}
  File "/home/patrick/enviroments/venv/lib/python3.5/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 23, in block "body"
    {% block content -%}
  File "/home/patrick/process_tracker/api/templates/index.html", line 58, in block "content"
    <!-- {{form.hidden_tag()}} {{wtf.form_field(form.pipeline)}} -->
  File "/home/patrick/enviroments/venv/lib/python3.5/site-packages/jinja2/runtime.py", line 553, in _invoke
    rv = self._func(*arguments)
  File "/home/patrick/enviroments/venv/lib/python3.5/site-packages/flask_bootstrap/templates/bootstrap/wtf.html", line 36, in template
    {% if field.flags.required and not required in kwargs %}
  File "/home/patrick/enviroments/venv/lib/python3.5/site-packages/jinja2/environment.py", line 430, in getattr
    return getattr(obj, attribute)
4

1 回答 1

0

根据您的错误消息,此字段

class pipelineForm(FlaskForm):
    pipeline = IntegerField('Pipeline ID')

是类型pewee.IntegerField,你希望它是IntegerFieldWTForms 的类型。如果您在同一个文件中有两个类,则需要:

import pewee
import wtforms.fields
class pipelineForm(FlaskForm):
    pipeline = fields.IntegerField('Pipeline ID')
class Process(Model):
    pipeline_id = pewee.IntegerField() # and so on
于 2017-06-22T20:10:01.283 回答