我在學習Flask,並嘗試構建一個非常簡單的應用程序,該應用程序創建新用戶和搜索現有用戶的部分很少。創建/註冊新用戶部分工作得很好,但由於某種原因,搜索功能沒有按預期工作,因爲表單沒有被提交。我試圖尋找我想到的每一個可能的情況,搜索了Stackoverflow,但無法修復它。不提交的表格
登記功能的代碼:
forms.py
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired, length
class Register(FlaskForm):
name = StringField('name', validators=[DataRequired(), length(min=1, max=30)])
email = StringField('email', validators=[DataRequired()])
submit = SubmitField('Register')
class UserSeacrh(FlaskForm):
name = StringField('name', validators=[DataRequired()])
email = StringField('email', validators=[DataRequired()])
submit = SubmitField('Search')
views.py
@app.route("/register", methods=['GET', 'POST'])
def register():
form = Register()
if form.validate_on_submit():
db.create_all()
user = CreateUser(form.name.data, form.email.data)
db.session.add(user)
db.session.commit()
flash('User {} created successfully'.format(form.name.data))
return redirect(url_for("register"))
return render_template("register.html", form=form, title='New User Registration')
@app.route("/search", methods=['GET', 'POST'])
def user_profile():
form = UserSeacrh()
if form.validate_on_submit():
flash('{}'.format(user))
return redirect(url_for("home"))
#return render_template("users.html", users=user)
return render_template("userSearch.html", form=form)
register.html
<!DOCTYPE html>
{% extends "base.html" %}
{% block body %}
<form action="" method="post">
{{ form.hidden_tag() }}
<div>
<h4>User Registeration</h4>
<span>Name {{form.name}}</span>
<span style="color: red">
{% for error in form.name.errors %}
{{error}}
{% endfor %}
</span
<span>Email {{form.email}}</span>
<span style="color: red">
{% for error in form.email.errors %}
{{error}}
{% endfor %}
</span>
</div>
{{form.submit()}}
</form>
{% with messages = get_flashed_messages() %}
{% if messages %}
<br/>
<ul>
{% for message in messages %}
<li>{{message}}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
{% endblock %}
userSearch.html
<!DOCTYPE html>
{% extends "base.html" %}
{% block body %}
<form action="" method="post">
{{ form.hidden_tag() }}
<h4>User Search Form</h4>
<div>
<span>Email {{form.email}}</span>
<span style="color: red">
{% for error in form.email.errors %}
{{error}}
{% endfor %}
</span>
</div>
{{form.submit()}}
</form>
{% with messages = get_flashed_messages() %}
{% if messages %}
<br/>
<ul>
{% for message in messages %}
<li>{{message}}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
{% endblock %}
我現在毫無章法,有人可以請幫我在這裏?
什麼是錯誤?當你按下搜索按鈕時,你會看到什麼? – Nurjan
完全沒有錯誤,只是它根本不執行任何操作。 – Rohit
看看我的答案。這可能會幫助你。您可能需要刪除UserSearch類中的'name'字段。 – Nurjan