为什么我会遇到错误:&quot'jinja2.exceptions.undefinederror:' form'是不确定的。在烧瓶/python/html项目上?
我正在创建一个烧瓶/Python/HTML项目,该项目向用户询问其名称。我正在尝试使用WTForms验证输入。
这是html(mainscraper.html):
{% extends 'base.html' %}
{% block head %}
<link rel ="stylesheet" href = "mainScraper.css">
{% endblock %}
{% block body %}
<h1> This is the main page </h1>
<p> Please enter your name: </p>
<br/>
<form action = "mainScraper", method = "POST">
{{ form.csrf_token }}
{{ form.username.label }}
{{ form.username }}
{{ form.submit }}
</form>
{% endblock %}
这是烧瓶/python:
from flask import Flask, render_template, request, redirect, url_for, session, flash
from flask_wtf import FlaskForm
from wtforms import SubmitField, StringField
from wtforms.validators import data_required, length
app = Flask(__name__)
app.secret_key = "34Secret56"
@app.route('/')
def index():
return render_template('mainScraper.html')
name = ' '
class NameForm(FlaskForm):
username = StringField('Name', validators = [data_required(),length(min = 1)])
submit = SubmitField('Submit')
@app.route('/mainScraper', methods = ['POST', 'GET'])
def mainScraper():
form = NameForm()
if form.validate_on_submit():
global name
name = form.username.data
return render_template('general.html')
return render_template('mainScraper.html', form=form)
我要确保至少要提交一个字符,然后再转移到下一个HTML页面(从mainscraper.html到General.html)。
但是我从mainscraper.html文件中收到此错误:“ jinja2.exceptions.undefinederror:'form'''是未定义的,我对为什么感到困惑,因为我在python文件的相应方法中定义了'form' ,主流。而且我不确定我还应该如何在HTML中验证我的
表格Python文件的IDE,但对HTML文件进行了反应,这是一个问题吗? 如果是这样,您建议我在Python和HTML文件中使用什么IDE?
I'm creating a flask/python/html project that asks the user for their name. I'm trying to use wtforms to validate the input.
This is the html (mainScraper.html):
{% extends 'base.html' %}
{% block head %}
<link rel ="stylesheet" href = "mainScraper.css">
{% endblock %}
{% block body %}
<h1> This is the main page </h1>
<p> Please enter your name: </p>
<br/>
<form action = "mainScraper", method = "POST">
{{ form.csrf_token }}
{{ form.username.label }}
{{ form.username }}
{{ form.submit }}
</form>
{% endblock %}
And this is the flask/python:
from flask import Flask, render_template, request, redirect, url_for, session, flash
from flask_wtf import FlaskForm
from wtforms import SubmitField, StringField
from wtforms.validators import data_required, length
app = Flask(__name__)
app.secret_key = "34Secret56"
@app.route('/')
def index():
return render_template('mainScraper.html')
name = ' '
class NameForm(FlaskForm):
username = StringField('Name', validators = [data_required(),length(min = 1)])
submit = SubmitField('Submit')
@app.route('/mainScraper', methods = ['POST', 'GET'])
def mainScraper():
form = NameForm()
if form.validate_on_submit():
global name
name = form.username.data
return render_template('general.html')
return render_template('mainScraper.html', form=form)
I want to make sure at least one character is submitted before moving on to the next html page (from the mainScraper.html to the general.html).
But I'm getting this error from my mainScraper.html file: "jinja2.exceptions.UndefinedError: 'form' is undefined" and I'm confused as to why, because I defined 'form' in the corresponding method of the python file, mainScraper. And I’m not sure how else I should validate my forms in html because most online videos or websites I’ve seen use flask-WTF or WTForms with {{ form.csrf_token}} in a html file
Also I’m not using an IDE for the python file, but React for the html file, is that a problem?
If so, what IDE do you suggest I use for the python and html file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论