form.validate_on_submit() doesnt work(nothing happen when I submit a form)

2024/10/5 18:34:28

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: 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!

Answer

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

https://en.xdnf.cn/q/119037.html

Related Q&A

How to find determinant of matrix using python

New at python and rusty on linear Algebra. However, I am looking for guidance on the correct way to create a determinant from a matrix in python without using Numpy. Please see the snippet of code belo…

How do I pass variables around in Python?

I want to make a text-based fighting game, but in order to do so I need to use several functions and pass values around such as damage, weapons, and health.Please allow this code to be able to pass &qu…

How to compare an item within a list of list in python

I am a newbie to python and just learning things as I do my project and here I have a list of lists which I need to compare between the second and last column and get the output for the one which has t…

Make for loop execute parallely with Pandas columns

Please convert below code to execute parallel, Here Im trying to map nested dictionary with pandas column values. The below code works perfectly but consumes lot of time. Hence looking to parallelize t…

Pre-calculate Excel formulas when exporting data with python?

The code is pulling and then putting the excel formulas and not the calculated data of the formula. xxtab.write(8, 3, "=H9+I9")When this is read in and stored in that separate file, it is sto…

Validating Tkinter Entry Box

I am trying to validate my entry box, that only accepts floats, digits, and operators (+-, %). But my program only accepts numbers not symbols. I think it is a problem with my conditions or Python Rege…

Imaplib with GMail offsets uids

Im querying my gmail inbox using pythons ImapLib with a range parameter, but my returned uids are offset from what I request. My request is as follows:M = imaplib.IMAP4_SSL(imap.gmail.com) M.login(USER…

Accessing a folder containing .wav files [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

What is the right Python idiom for sorting by a single criterion (field or key)? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 8…

Incorrect checking of fields in list using a for loop

I have the following code that seeks to read the file contents into a list (this bit works) and then display a message of acceptance, IF the bank details (user and corresponding number) matches. e.g. i…