【问题标题】:Flask jinja2.exceptions.UndefinedError: 'form' is undefined* [RESOLVED]烧瓶 jinja2.exceptions.UndefinedError: 'form' is undefined* [已解决]
【发布时间】:2021-10-03 09:22:35
【问题描述】:

编辑: 伙计们,三个小时后我解决了这个问题...... 问题出在 html 依赖 **{{form.RegcheckEmial(name='email',placeholder='Email')}}** name='email' 告诉我我的错误是这样解决的:

在 reg.html 中

我删除了

{{form.RegcheckEmial(name='email',placeholder='Email')}}

替换这个

{{form.RegcheckEmial(placeholder='Email')}}</br> 

在 file.py 中

我删除了:

Email=request.form['email']
username=request.form['username']
password=request.form['password']

并替换了这个:

   email=form.RegcheckEmial.data   
   user=form.Regcheckuser.data
   password=form.Recheckpassword.data

dHi 伙计们,我的烧瓶有问题,控制台给我错误,例如 我不知道如何解决这个问题,我的 html 说我的表单未定义 我想检查输入数据的验证,如果数据已通过验证成功输入,请转到将数据发送到我的数据库

class LoginForm(FlaskForm):
    checkEmial = StringField('checkEmial', validators=[InputRequired('A username is required!'), Length(min=5, max=50, message='Must be between 5 and 10 characters.'),Email(message='PLS ENETER CORRET Email')])
    checkPassword = PasswordField('checkPassword', validators=[InputRequired('Password is required!'),Length(min=6, max=30, message='type the stronger password')])

class RegisterForm(FlaskForm):
          RegcheckEmial=StringField('checkkEmiall',validators=[InputRequired('Please type value'),Length(min=5, max=50, message='Must be between 5 and 10 characters.'),Email(message='PLS ENETER CORRET Email')])

文件.py

@app.route('/reg.html',methods=['POST'])
def Register():
    form=RegisterForm()        
    if form.validate_on_submit():
      if request.method=='POST':         
       Email=request.form['email']
   username=request.form['username']
   password=request.form['password']
            
   cur=mysql.connection.cursor()
   cur.execute('''INSERT INTO dane (Email,username,UserPassword) VALUES (%s , %s , %s)''',(Email , username , password))
   mysql.connection.commit() 
   cur.close()
   return redirect(url_for('index'))
   return render_template('reg.html',form=form)

reg.html

<div class="LoginSquare">
{% block content %}
    <form method='POST' >




                                    
{{ form.csrf_token }}
{{form.RegcheckEmial(name='email',placeholder='Email')}}  \
{% for error in form.RegcheckEmial.errors %}
<span style="color:red;">{{ error }}</br></span>
{% endfor %}
<div class="d1">
<input type='submit'name='Register' id='awdwd'class='wdawd'>
</div>
</form>
{%endblock%}

**index.html//(

this is my login page where I use LoginForm classes )**

 <form method='POST' action="{{ url_for('index') }}" >
                              <div class="page1">
                                    
                                    <img  src="/static/pages/useerr.png" width="50px" height="50px">
                              
                              </div>
                              <!-- {{form.csrf_token}} -->
                             
                              {{ form.csrf_token }}
                             
                              {{ form.checkEmial(placeholder='Email')}}</br>
                              
                              {% for error in form.checkEmial.errors %}
                                  <span style="color:red;">{{ error }}</br></span>
                              {% endfor %}

错误控制台:jinja2.exceptions.UndefinedError: 'form' is undefined, in top-level template code {% 块内容 %},文件“c:\python39\lib\site-packages\jinja2\environment.py”,第 474 行,在 getattr 返回 getattr(obj, 属性)

【问题讨论】:

    标签: python flask


    【解决方案1】:

    你的代码有两个问题。

    1. 您不必使用文件名作为路径。它可以像“/register”或“/signup”,如果您使用/register 作为路由,您将能够通过转到www.site.com/register 之类的url 来访问页面。
    2. 您只允许对 url 的 post 请求,您将无法访问该站点。它会给你错误405 method not allowed错误。您需要允许get 请求网址。

    所以你的路线应该是:

    @app.route('/register',methods=['POST','GET'])
    

    您可以前往yoursite.com/register访问该页面。

    另外,我假设您访问了错误的网址,因为您没有在问题中提供您没有向模板提供 form 变量的问题。

    【讨论】:

    • 是的,谢谢,但现在我有错误 TypeError: wtforms.widgets.core.html_params() got multiple values for keyword argument 'name' :c
    • 你能编辑你的问题并粘贴表单类吗?
    • 是的,我还添加了另一个类“LoginForm”,它可以帮助我检查数据库中的数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多