Python form.validate_on_submit()不';t工作(当我提交表格时没有发生任何事情)
我正在为社交媒体网站创建一个博客发布功能,但我遇到了一个问题:当我点击“发布”按钮(在create_Post.html上)时,什么也没发生 在我的blog_posts/views.py中,当form.validate_on_submit()时,应该保存用户填写的数据,然后他或她将通过指向index.html的路径重定向 然而,通过运行该网站,什么都没有发生。奇怪的是,没有发生错误 以下是我的博客\u post/views.py的代码:Python form.validate_on_submit()不';t工作(当我提交表格时没有发生任何事情),python,html,database,flask,model,Python,Html,Database,Flask,Model,我正在为社交媒体网站创建一个博客发布功能,但我遇到了一个问题:当我点击“发布”按钮(在create_Post.html上)时,什么也没发生 在我的blog_posts/views.py中,当form.validate_on_submit()时,应该保存用户填写的数据,然后他或她将通过指向index.html的路径重定向 然而,通过运行该网站,什么都没有发生。奇怪的是,没有发生错误 以下是我的博客\u post/views.py的代码: @blog_posts.route('/create', m
@blog_posts.route('/create', methods=['GET', 'POST'])
@login_required
def create_post():
form = BlogPostForm()
if form.validate_on_submit():
blog_validated = BlogPost(problem_name=form.problem_name.data,
text=form.text.data,
user_id=current_user.id
)
db.session.add(blog_validated)
db.session.commit()
flash('Blog Post Created')
return redirect(url_for('core.index'))
return render_template('create_post.html', form=form)
下面是我的forms.py代码,其中包含我用于模板的表单(create_post.html):
以下是我的模板代码(create_post.html):
这是我单击post按钮时的终端:
另外,在我查看代码之后,我认为很有可能我没有将视图与数据库(BlogPost.db.Model)连接起来。我是一名学生,学校里没有人能帮助我(甚至我的计算机科学老师)。如果你能帮助我,我将不胜感激!!谢谢
如果你需要更多的信息,请问我 我解决了这个错误!!!!!!!!{{form.problem\u submit.label(class=“btn btn secondary”)}}应该是{{form.problem\u submit(class=“btn btn secondary”)}你按下提交表单的按钮的代码在哪里?我想是这个吗?forms.py中的problem_submit=SubmitField(“Post”)和create_Post.html中的{form.problem_submit.label(class=“btn btn secondary”)}}。我想当我说form.validate_on_submit()时,这意味着用户单击了{{form.problem_submit.label(class=“btn btn secondary”)}。您能告诉我如何创建db.session吗?嗨!谢谢你帮助我!我在整个项目的init.py中将数据库注册为:db=SQLAlchemy(app)。“app”是我用于运行项目的名称:app=Flask(name)通过python app.py。据我所知,db.session临时存储数据,以便它可以将数据迁移到其他文件。嗨,dhentris,我很抱歉,但我遇到了一个新问题,如果您能帮助我,那将非常棒:谢谢!
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, TextAreaField
from wtforms.validators import DataRequired
class BlogPostForm(FlaskForm):
problem_name = StringField('Problem Name', validators=[DataRequired()])
text = TextAreaField('Text', validators=[DataRequired()])
problem_submit = SubmitField("Post")
{% extends "base.html" %}
{% block content %}
<div class="container">
<form method="POST" >
{{ form.hidden_tag() }}
{{ form.problem_name.label(class="form-control-label") }} {{ form.problem_name(class="form-group form-control") }}<br>
{{ form.text.label }} {{ form.text(class="form-group form-control") }}<br>
{{ form.problem_submit.label(class="btn btn-secondary")}}
</form>
</div>
{% endblock %}
class BlogPost(db.Model):
users = db.relationship(User)
blog_id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer,db.ForeignKey('users.id'), nullable=False) #users.id is taken from the tablename(users) and id in its table
date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) #Automatically post the time of the post
problem_name = db.Column(db.String(140), nullable=False)
text = db.Column(db.Text, nullable=False)
def __init__(self, text, problem_name, user_id):
self.text = text
self.problem_name = problem_name
self.user_id = user_id
def __repr__(self):
return f"Post ID: {self.post_id} -- Date:{self.date}---{self.problem_name}"