客户端验证设置 required="" 仍然要求输入

app.py文件:

# -*- coding:utf-8 -*-
import os

from flask import Flask, render_template, flash, redirect, url_for, request, send_from_directory
from flask import session

from forms import LoginForm

app = Flask(__name__)
app.secret_key = os.getenv('SECRET_KEY', 'secret string')
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True


@app.route('/', methods=['GET', 'POST'])
def index():
    return render_template('index.html')


@app.route('/html', methods=['GET', 'POST'])
def html():
    return render_template('pure_html.html')


@app.route('/basic', methods=['GET', 'POST'])
def basic():
    form = LoginForm()
    return render_template('basic.html', form=form)

froms.py文件:

# -*- coding: utf-8 -*-
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import DataRequired, Length


class LoginForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired()])
    password = PasswordField('Password', validators=[DataRequired(), Length(2, 64)])
    remember = BooleanField('Remember me')
    submit = SubmitField('Log in')

basic.html文件:

{% extends 'base.html' %}

{% block content %}
<h2>Basic Form</h2>

<form method="post">
    {{ form.csrf_token }}

    {{ form.username.label }}<br>
    {{ form.username(required='') }}<br>

    {{ form.password.label }}<br>
    {{ form.password }}<br>

    {{ form.remember }}{{ form.remember.label }}<br>

    {{ form.submit }}<br>
</form>
{% endblock %}

最后在页面源码中有required属性,但是点击的时候显示默认的提示,而且之前是英文的,现在不知道怎么就变成中文的了:

image

username的required的设置为空了,为什么页面的提示还是没变?

http://www.w3school.com.cn/tiy/t.asp?f=html5_input_required 你自己打开这个网页试试就知道了:grinning:

2 个赞

明白了,谢谢。

请参考 论坛说明 了解如何为代码块设置语法高亮。