UndefinedError:' login_user_form'未定义

时间:2016-05-26 11:41:34

标签: html flask

目前,我正在建立一个用于练习目的的网站。我正在使用Flask-security来实现登录和注册系统。我收到了标题中所述的错误。这是我的代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Main</title>
    <meta name=":viewport" content="width=device-width, initial-scale =1">
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-responsive.min.css">

    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">

    <!--NavBar Design-->
    <link href="{{url_for('static',filename='navbar_design.css')}}" rel="stylesheet">
</head>
<body>

<!--HEADER-->
<nav class="navbar navbar-custom navbar-static-top">
 <div class="container-fluid">
    <div class="navbar-header">

        <img style="max-width:80px; " src="{{url_for('static',filename='v4.png')}}" >

    </div>
     <ul class="nav navbar-nav">
      <li class="active"><a style="font-size:25px; margin-top:19px;" href="#">About Us</a></li>
      <li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#" style="font-size:25px; margin-top:19px; " >Main Menu <span class="caret"></span></a>
        <ul class="dropdown-menu">
          <li><a href="#">Survey Application Form</a></li>
          <li><a href="#">Check The Result of The Survey Application</a></li>

        </ul>
      </li>
    </ul>
      <ul class="nav navbar-nav navbar-right">
          <li><a style="font-size:21px; margin-top:14px;" href="#" data-toggle="modal" data-target="#myModal1"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
          <li><a style="font-size:21px; margin-top:14px;" href="#" data-toggle="modal" data-target="#myModal2"><span class="glyphicon glyphicon-log-in"></span> Log In</a></li>
    </ul>
 </div>
</nav>

<!--body-->
{% from "security/_macros.html" import render_field_with_errors, render_field %}
{% include "security/_messages.html" %}

<div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <!--Modal Head-->
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h2 class="modal-title" id="myModalLabel">Log In</h2>
        </div>

<!--Modal Body-->
      <div claas = "modal-body">
        <form action="{{ url_for_security('login') }}" method="POST" name="login_user_form">
              {{ login_user_form.hidden_tag() }}
              {{ render_field_with_errors(login_user_form.email, class="form-control") }}
              {{ render_field_with_errors(login_user_form.password, class="form-control") }}
              {{ render_field_with_errors(login_user_form.remember ) }}
              {{ render_field(login_user_form.next) }}
              <div class="modal-footer">
                {{ render_field(login_user_form.submit, class="btn btn-success") }}
              </div>

        </form>
        Dont have an account ?{% include "security/_menu.html" %}

      </div>
   </div>
  </div>
</div>




<!--Footer-->
<div class = "navbar navbar-custom navbar-fixed-bottom" role="navigation">
 <div class = "container-fluid">
</div>

</div>

<!-- Latest compiled and minified JavaScript -->
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

</body>
</html>

在我的烧瓶里面,我有类似的东西

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

这个错误会给我错误。但是,如果我更改了模板,然后通过更改URL访问登录,例如https://127.0.0.1:5000/login。这种方法会产生NO错误,但这不是我想要的,我希望以上能够运行。任何帮助将不胜感激 !谢谢!

编辑: screenshots of the error

1 个答案:

答案 0 :(得分:0)

您可以发布错误追溯。

您的索引功能看起来并未将适当的参数传递给模板。

@app.route('/')
def index():
    login_user_form = LoginForm()
    return render_template('main.html',login_user_form)

修改

LoginForm类应如下所示。

class LoginForm(Form):
    username = StringField("Enter your username",validators=[DataRequired()])
    password = PasswordField("Enter your password",validators=[DataRequired()])
    submit = SubmitField("Submit")

有关详细信息,请查看此链接 - Flask-wtf quickstart

希望它有所帮助。