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 DataRequiredclass 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 tabledate = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) #Automatically post the time of the postproblem_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 = textself.problem_name = problem_nameself.user_id = user_iddef __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: 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!