TJH
TJH

Reputation: 11

why isn't my if statement working?

I need user input to save to a database and then display on an app. If the user's input is left blank, I need to display an error message.

I'm running into trouble with the error messages displaying at all. When user input is blank, a blank post is saved to the db and then displayed with no content rather than displaying the error message.

I am using Python, Jinja2, Flask, HTML

Thanks!!

    if request.method == 'POST':

        blog_title = str(request.form['title'])
        blog_body = str(request.form['body'])

        title_error = ''
        body_error = ''

        if len(blog_title) == 0:
            title_error = 'Please give your blog a title'
            return title_error

        if len(blog_body) == 0:
            body_error = "Please give your blog some content"
            return body_error
        else:
            new_blog = Blog(blog_title, blog_body)
            db.session.add(new_blog)
            db.session.commit()

            blog_id = new_blog.id

            return redirect(url_for('blogs', id=blog_id))

    else:
        return render_template('/newpost_form.html')

Upvotes: 0

Views: 124

Answers (2)

Adam
Adam

Reputation: 4172

I would consider integrating Flask-WTF. I only do flask forms that way. Provides a lot of validation tools that make it easy to work with

forms.py

from flask_wtf import FlaskForm
from wtf.fields import StringField, TextAreaField, SubmitField
from wtf.validators import DataRequired

class NewBlogForm(FlaskForm):
    title = StringField('Title', validators=[DataRequired(message='one')])
    body = TextAreaField('Body', validators=[DataRequired(message='two')])
    submit = SubmitField('Save')

routes.py

from forms import NewBlogForm

@app.route('/newpost', methods=['GET', 'POST'])
def new_post():
    form = NewBlogForm()
    if form.validate_on_submit():
        blog = Blog(form.title.data, form.body.data)
        db.session.add(blog)
        db.session.commit()  # should have try block to catch any errors
        return redirect(url_for('blogs', id=blog.id))
    return render_template('newposts_form.html', form=form)

newposts_form.html

<form>
    <ul>
    {% for error in form.errors %}
        <li>{{ error }}</li>
    {% endfor %}
    </ul>
    {{ form.csrf_token }}
    {{ form.title.label }}{{ form.title }}
    {{ form.body.label }}{{ form.body }}
    {{ form.submit }}
</form>

If you are using Flask-Bootstrap they support wtf forms and show errors in a really clean way.

Upvotes: 1

developer_hatch
developer_hatch

Reputation: 16224

you are reasinging the values of the variables, do the following:

if request.method == 'POST':

    blog_title = str(request.form['title'])
    blog_body = str(request.form['body'])

    if len(blog_title) == 0:
        return 'Please give your blog a title'
    elif len(blog_body) == 0:
        return "Please give your blog some content"
    else:
        new_blog = Blog(blog_title, blog_body)
        db.session.add(new_blog)
        db.session.commit()

        blog_id = new_blog.id

        return redirect(url_for('blogs', id=blog_id))

else:
    return render_template('/newpost_form.html')

Upvotes: 0

Related Questions