Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

python - form.validate_on_submit() doesn't work(nothing happen when I submit a form)

I'm creating a posting blog function for social media website and I'm stuck on a problem: when I click on the "Post" button(on create_post.html), nothing happens.

In my blog_posts/views.py, when form.validate_on_submit(), the data the users filled in should be saved and then he or she will be redirected through a route toward index.html.

However, by running the website, nothing happened. Strangely, there is no error that happened.

Here is code for my blog_post/views.py:

@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)

Here is my code for my forms.py that have the form I use for my template (create_post.html):

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")

Here is my code for the template(create_post.html):

{% 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 %}

Here is my code for my model.py(database) incase you need to know something:

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}"

This is the terminal when I click the post button: Terminal when click the button P.s/ After I looked at my code, I think that there is a high possibility I didn't connect my views with my database(BlogPost.db.Model). I'm a student and no one in my school can help me(even my computer science teacher). If you could help me, I would greatly appreciate!! Thank you!!

And if you need more information, please ask me!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I solved the bug!!!!!!!! {{form.problem_submit.label(class="btn btn-secondary")}} should had been {{form.problem_submit(class="btn btn-secondary")}}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...