为什么我会遇到错误:&quot'jinja2.exceptions.undefinederror:' form'是不确定的。在烧瓶/python/html项目上?

发布于 2025-02-11 14:31:59 字数 1797 浏览 0 评论 0原文

我正在创建一个烧瓶/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文