Flask Blueprints by 2015
Author:2015
Language: eng
Format: mobi, epub
Publisher: Packt Publishing
The last bit that we need is the view handler to add a new post. As we haven't created the application/posts/views.py module, let's do that. We'll need a Flask-WTForm class to handle/validate the new posts and a route handler to send and process the required fields, all hooked up to a new blueprint:
from flask import Blueprint, render_template, url_for, redirect, flash, current_app from flask.ext.login import login_required, current_user from flask.ext.wtf import Form from wtforms import StringField from wtforms.widgets import TextArea from wtforms.validators import DataRequired from sqlalchemy import exc from models import Post from application import db posts = Blueprint('posts', __name__, template_folder='templates') class CreatePostForm(Form): """Form for creating new posts.""" content = StringField('content', widget=TextArea(), validators=[DataRequired()]) @posts.route('/add', methods=['GET', 'POST']) @login_required def add(): """Add a new post.""" form = CreatePostForm() if form.validate_on_submit(): user_id = current_user.id post = Post(user_id=user_id, content=form.content.data) db.session.add(post) try: db.session.commit() except exc.SQLAlchemyError: current_app.exception("Could not save new post!") flash("Something went wrong while creating your post!") else: return render_template('posts/add.html', form=form) return redirect(url_for('users.feed'))
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9918)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9797)
The Mikado Method by Ola Ellnestam Daniel Brolund(9780)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8303)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7784)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7764)
Grails in Action by Glen Smith Peter Ledbrook(7699)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7558)
Windows APT Warfare by Sheng-Hao Ma(6868)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6602)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6470)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6420)
Kotlin in Action by Dmitry Jemerov(5068)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4320)
Functional Programming in JavaScript by Mantyla Dan(4040)
Solidity Programming Essentials by Ritesh Modi(4021)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3812)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3755)
The Ultimate iOS Interview Playbook by Avi Tsadok(3730)
