0

I am trying to create a simple form using Flask in python. I am trying to use jinja2 template to display the form. But the template objects are not creating corresponding html elements and just printing the form field object as text.

This is the flask code block

from flask import Flask
from flask import render_template
from flask_wtf import FlaskForm
from wtforms import IntegerField

app = Flask(__name__)

class TestForm(): 
    value = IntegerField('Value')


@app.route('/', methods = ['GET', 'POST'])
def test():
    
    form = TestForm()
    
    return render_template('template.html', form = form)

This is the html template I have

<!DOCTYPE html>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <p>Test Page !!</p>
    <div>
        <form method="post">
            {{ form.csrf_token }}
            {{ form.value.label }}
            {{ form.value }}
            <input type="submit">
        </form>
    </div>
</body>
</html>

This is how the webpage looks like

enter image description here

I am not sure why the html elements corresponding to the form are not displaying. Any idea why this might be happening. I am trying to run this on Windows in an anaconda python environment. I haven't had issues previously on ubuntu.

CodeLikeBeaker
  • 20,682
  • 14
  • 79
  • 108
sibiyes
  • 61
  • 7
  • Check out this question/answer. It may have to deal with your version of wtforms, or how you're rendering the form: https://stackoverflow.com/questions/16406496/rendering-forms-with-flask-wtform – CodeLikeBeaker Jan 08 '21 at 21:35

1 Answers1

0

Your form should be inheriting from FlaskForm, like so:

from flask_wtf import FlaskForm

class TestForm(FlaskForm): 
    value = IntegerField('Value')
Karl Sutt
  • 575
  • 3
  • 11