form is undefined flask
@app.route('/', methods = ['GET', 'POST']) @app.route('/index', methods = ['GET', 'POST']) @login_required def index(): form = PostForm() if form.validate_on_submit(): post = Post(body = form.post.data, timestamp = datetime.utcnow(), author = g.user) db.session.add(post) db.session.commit() flash('Your post is now live!') return redirect(url_for('index')) posts = g.user.followed_posts().all() return render_template("index.html", title = 'Home', user = user, posts = posts, form = form)
Here is what the above code is Doing:
1. The route decorator creates a mapping between the URL /login and the login function.
2. When Flask receives a request to /login, it will call the login function and pass the return value of it back to the browser as a response.
3. The login function is very similar to the index function. It creates a response with the rendered template.
4. The render_template function invokes the Jinja2 template engine that comes bundled with the Flask framework. Jinja2 substitutes {{ … }} blocks with the corresponding values, given by the arguments provided in the render_template call.
5. The render_template call invokes the Jinja2 template engine that comes bundled with the Flask framework. Jinja2 substitutes {{ … }} blocks with the corresponding values, given by the arguments provided in the render_template call.
6. The render_template function invokes the Jinja2 template engine that comes bundled with the Flask framework. Jinja2 substitutes {{ … }} blocks with the corresponding values, given by the arguments provided in the render_template call.