Flask - 表单数据是 None?
Flask - Form Data is None?
我正在尝试设置硬编码身份验证,只是为了在我的 def login()
路由中进行测试,但是 forms.user_email.data
/forms.user_password.data
返回 None
?我也注意到我的 def home()
中的 form
表现出相同的行为。我相信这是多个问题的根源,但老实说我不确定。我已经阅读了多篇关于使用请求的帖子 - 但我一直看到其他人使用 FlaskForm 来实现这一点?
非常感谢任何指导,以及任何一般的 Flask 提示 - 我对 Python/Flask 很陌生。我一直很难理解 flask/flask_restful 的最佳 practices/use-cases,更不用说所有可能的扩展,如 flask_bootstrap、FlaskForm、WTForms、FlaskLogin 等。提前致谢。
from flask import Flask, render_template, url_for, redirect, flash
from flask_restful import Resource, Api
from flask_wtf import FlaskForm
import requests
import pandas as pd
from flask_bootstrap import Bootstrap
from wtforms import EmailField, PasswordField, StringField
from wtforms import validators
app = Flask(__name__)
api = Api(app)
app.config['SECRET_KEY'] = 'd43b80c0727dca296c607fe8f8db478264'
bootstrap = Bootstrap(app)
class LoginForm(FlaskForm):
user_email = EmailField('Email', validators=[validators.DataRequired()])
user_password = PasswordField('Password', validators=[validators.DataRequired()])
class InputForm(FlaskForm):
ticker = StringField('Ticker', validators=[validators.DataRequired()])
class GetStonks(Resource):
def get(self, ticker):
HEADER = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0"}
try:
response = requests.get(f"https://finviz.com/quote.ashx?t={ticker}", headers=HEADER)
result = pd.read_html(response.text)
parsed = result[5]
dict_primary = {}
dict_primary |= dict(parsed[[0,1]].values)
dict_primary |= dict(parsed[[2,3]].values)
dict_primary |= dict(parsed[[4,5]].values)
dict_primary |= dict(parsed[[6,7]].values)
dict_primary |= dict(parsed[[8,9]].values)
dict_primary |= dict(parsed[[10,11]].values)
return dict_primary, 200
except IndexError:
return {"Message": "Ticker ID not found."}
api.add_resource(GetStonks, "/api/v1/stocks/<string:ticker>")
@app.route('/')
@app.route("/login", methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
if form.user_email.data == 'admin@admin.com' and form.user_password.data == 'admin':
return redirect(url_for('home'))
else:
flash('Authentication failure', 'danger')
return render_template('login.html', title='Login', form=form)
@app.route("/home", methods=['GET', 'POST'])
def home():
form = InputForm()
return render_template('home.html', title='Home', form=form)
if __name__ == '__main__':
app.run(debug=True)
home.html
{% extends "bootstrap/base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% import "bootstrap/utils.html" as utils %}
{% block title %}
Home
{% endblock %}
{{super()}}
<link rel="stylesheet" href="{{url_for('.static', filename='signin.css')}}">
{% block body %}
<div class="container">
<form class="form-signin" method="POST" action="http://127.0.0.1:5000/api/v1/stocks/{{url}}">
<h2 class="form-signin-heading">INPUT TICKER BELOW</h2>
<br>
{{ form.hidden_tag() }}
{{ form.csrf_token }}
{{ wtf.form_field(form.ticker) }}
<button class="btn btn-lg btn-primary" type="submit">GET</button>
</form>
</div>
{% endblock %}
login.html
{% extends "bootstrap/base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% import "bootstrap/utils.html" as utils %}
{% block title %}
Login
{% endblock %}
{{super()}}
<link rel="stylesheet" href="{{url_for('.static', filename='signin.css')}}">
{% block content %}
<div class="container">
<form class="form-signin" method="POST" action="{{ url_for('home') }}">
<h2 class="form-signin-heading">SIMPLE STOCK API</h2>
<br>
<br>
{{ form.hidden_tag() }}
{{ form.csrf_token }}
{{ wtf.form_field(form.user_email) }}
{{ wtf.form_field(form.user_password) }}
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
{% endblock %}
在你的 login.html
而不是:
<form class="form-signin" method="POST" action="{{ url_for('home') }}">
操作应该是 login
而不是 home
。所以它应该是这样的:
<form class="form-signin" method="POST" action="{{ url_for('login') }}">
我正在尝试设置硬编码身份验证,只是为了在我的 def login()
路由中进行测试,但是 forms.user_email.data
/forms.user_password.data
返回 None
?我也注意到我的 def home()
中的 form
表现出相同的行为。我相信这是多个问题的根源,但老实说我不确定。我已经阅读了多篇关于使用请求的帖子 - 但我一直看到其他人使用 FlaskForm 来实现这一点?
非常感谢任何指导,以及任何一般的 Flask 提示 - 我对 Python/Flask 很陌生。我一直很难理解 flask/flask_restful 的最佳 practices/use-cases,更不用说所有可能的扩展,如 flask_bootstrap、FlaskForm、WTForms、FlaskLogin 等。提前致谢。
from flask import Flask, render_template, url_for, redirect, flash
from flask_restful import Resource, Api
from flask_wtf import FlaskForm
import requests
import pandas as pd
from flask_bootstrap import Bootstrap
from wtforms import EmailField, PasswordField, StringField
from wtforms import validators
app = Flask(__name__)
api = Api(app)
app.config['SECRET_KEY'] = 'd43b80c0727dca296c607fe8f8db478264'
bootstrap = Bootstrap(app)
class LoginForm(FlaskForm):
user_email = EmailField('Email', validators=[validators.DataRequired()])
user_password = PasswordField('Password', validators=[validators.DataRequired()])
class InputForm(FlaskForm):
ticker = StringField('Ticker', validators=[validators.DataRequired()])
class GetStonks(Resource):
def get(self, ticker):
HEADER = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0"}
try:
response = requests.get(f"https://finviz.com/quote.ashx?t={ticker}", headers=HEADER)
result = pd.read_html(response.text)
parsed = result[5]
dict_primary = {}
dict_primary |= dict(parsed[[0,1]].values)
dict_primary |= dict(parsed[[2,3]].values)
dict_primary |= dict(parsed[[4,5]].values)
dict_primary |= dict(parsed[[6,7]].values)
dict_primary |= dict(parsed[[8,9]].values)
dict_primary |= dict(parsed[[10,11]].values)
return dict_primary, 200
except IndexError:
return {"Message": "Ticker ID not found."}
api.add_resource(GetStonks, "/api/v1/stocks/<string:ticker>")
@app.route('/')
@app.route("/login", methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
if form.user_email.data == 'admin@admin.com' and form.user_password.data == 'admin':
return redirect(url_for('home'))
else:
flash('Authentication failure', 'danger')
return render_template('login.html', title='Login', form=form)
@app.route("/home", methods=['GET', 'POST'])
def home():
form = InputForm()
return render_template('home.html', title='Home', form=form)
if __name__ == '__main__':
app.run(debug=True)
home.html
{% extends "bootstrap/base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% import "bootstrap/utils.html" as utils %}
{% block title %}
Home
{% endblock %}
{{super()}}
<link rel="stylesheet" href="{{url_for('.static', filename='signin.css')}}">
{% block body %}
<div class="container">
<form class="form-signin" method="POST" action="http://127.0.0.1:5000/api/v1/stocks/{{url}}">
<h2 class="form-signin-heading">INPUT TICKER BELOW</h2>
<br>
{{ form.hidden_tag() }}
{{ form.csrf_token }}
{{ wtf.form_field(form.ticker) }}
<button class="btn btn-lg btn-primary" type="submit">GET</button>
</form>
</div>
{% endblock %}
login.html
{% extends "bootstrap/base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% import "bootstrap/utils.html" as utils %}
{% block title %}
Login
{% endblock %}
{{super()}}
<link rel="stylesheet" href="{{url_for('.static', filename='signin.css')}}">
{% block content %}
<div class="container">
<form class="form-signin" method="POST" action="{{ url_for('home') }}">
<h2 class="form-signin-heading">SIMPLE STOCK API</h2>
<br>
<br>
{{ form.hidden_tag() }}
{{ form.csrf_token }}
{{ wtf.form_field(form.user_email) }}
{{ wtf.form_field(form.user_password) }}
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
{% endblock %}
在你的 login.html
而不是:
<form class="form-signin" method="POST" action="{{ url_for('home') }}">
操作应该是 login
而不是 home
。所以它应该是这样的:
<form class="form-signin" method="POST" action="{{ url_for('login') }}">