0 Replies - 570 Views - Last Post: 12 September 2012 - 02:47 AM Rate Topic: -----

#1 Spykedjadedragon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 23-November 08

Need help with wtforms code in Python Flask app

Posted 12 September 2012 - 02:47 AM

Hi guys,

I'm trying to create a small inventory web app, and I'm using Python's micro framework Flask to do it. I need to create a form, so I'm using the wtforms module to help me generate it.

I wrote a basic class for my form in my __init__.py file, which you can see below:

from flask import Flask
from wtforms import Form, TextField, BooleanField, RadioField, IntegerField, TextAreaField, validators

app = Flask(__name__)

class InventoryForm(Form):
  item = TextField('Item', [validators.Length(min=2)])
  item_type = RadioField('Machine', 'Peripheral')
  number = IntegerField()
  notes = TextAreaField()

import inventoryapp.views

And in my views.py file, I have:

from inventoryapp import app
from flask import render_template
from wtforms import Form

@app.route('/', methods=['GET', 'POST'])
def index():
  form = InventoryForm(request.form)
  if request.method == 'POST' and form.validate():
    entry = Item(form.item.data, form.item_type.data, form.number.data, form.notes.data)
    flash('Item added.')
    return redirect(url_for('index'))
  return render_template('index.html', form=form)

Finally, I'm not sure if It's relevant but my index.html template looks like this:

<!DOCTYPE html>
<html lang="en">
        <meta charset="utf-8" />
        <title>IT Inventory</title>
        <link rel="stylesheet" type="text/css" href="../static/style.css" />

<body id="content">

        <h1>IT-Ops Inventory App</h1>

{% macro render_field(field) %}
  <dt>{{ field.label }}
  <dd>{{ field(**kwargs)|safe }}
  {% if field.errors %}
    <ul class=errors>
    {% for error in field.errors %}
      <li>{{ error }}</li>
    {%endfor %}
  {% endif %}
{% endmacro %}

{% from "_formhelpers.html" import render_field %}
<form method="post" action="/index">
  {{ render_field(form.item) }}
  {{ render_field(form.item_type) }}
  {{ render_field(form.number) }}
  {{ render_field(form.notes) }}
<p><input type=submit value=Submit>


Now, every time I try to run my package it gives me:

NameError: global name 'InventoryForm' is not defined

Maybe I'm not importing some module that I need correctly in my views.py?
HELP! I've been trying to figure this out for days. :(


Is This A Good Question/Topic? 0
  • +

Page 1 of 1