我尝试用flask编写简单的服务,但是有一些问题。我尝试在提交后渲染模板,但并没有达到某些目的
主要代码
@app.route('/', methods=['GET', 'POST'])
def main_page():
main_form = Main_Submit_Form()
text_form = Checking_Text_Form()
image_form = Checking_Image_Form()
if main_form.validate_on_submit():
if main_form.name_checking.data:
if text_form.validate_on_submit():
request_name = str(text_form.checking_name.data)
results = model.predict_name(request_name)
return render_template('text_data.html', request_name=request_name, results=results)
return render_template('login.html', title='Sign In', form=text_form)
if main_form.image_checking.data:
if image_form.validate_on_submit():
pass
return render_template('main.html', form=main_form)
主表单如下
from flask_wtf import FlaskForm
from wtforms import SubmitField
class Main_Submit_Form(FlaskForm):
name_checking = SubmitField(label='Проверка Торгового наименования')
image_checking = SubmitField(label='Проверка Логотипа')
文本形式如下
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, FileField
from wtforms.validators import DataRequired
class Checking_Text_Form(FlaskForm):
checking_name = StringField('Введите наименование компании', validators=[DataRequired()])
submit_text = SubmitField('Проверить')
主要html
{% extends "base.html" %}
{% block content %}
<h2>Проверка товарного знака</h2>
<form action="" method="post" novalidate>
{{ form.hidden_tag() }}
<p class="text">{{ form.name_checking() }}</p>
<p class="image">{{ form.image_checking() }}</p>
</form>
{% endblock %}
登录html
{% extends "base.html" %}
{% block content %}
<h2>Проверка товарного знака</h2>
<form action="" method="post" novalidate>
{{ form.hidden_tag() }}
<p>
{{ form.checking_name.label }}<br>
{{ form.checking_name(size=32) }}
</p>
<p>{{ form.submit_text() }}</p>
</form>
{% endblock %}
Textdata html
{% extends "base.html" %}
{% block content %}
<h2>Результаты по запросу: {{ request_name }}</h2>
{% for result in results %}
<div><p>{{ result.company_name }}, сходство: <b>{{ result.cosine_sim }}</b></p></div>
{% endfor %}
{% endblock %}
我无法检查if text_from.validate_on_submit()
,也不知道为什么。
渲染login.html
模板后如何进入该块?
有什么方法可以简化此代码吗?我认为这不是一个很好的例子