Flask-WTForms在提交时不传递数据。 AttributeError:'NoneType'对象没有属性'id'

时间:2017-11-03 04:46:47

标签: python flask-sqlalchemy flask-wtforms

我是Python和Flask框架的新手,我无法弄清楚如何将数据传递给表单。我一直得到AttributeError:当我提交表单时,'NoneType'对象没有属性'id'。

我能够将对象(invoice1)从视图传递到模板并显示对象属性。 (invoice.id)但我无法将对象传递给表单。

@app.route('/invoice/add', methods=['GET', 'POST'])
@login_required
def addItem():
    invoice2 = request.args.get('invoice_id', type=int)
    invoice1 = Invoice.query.filter_by(id=invoice2).first()
    form = ItemForm(obj=invoice1)
    if form.validate_on_submit():
        newItem = Item(
            invoice = invoice1,
            quantity = form.quantity.data,
            category = form.category.data,
            brand = form.brand.data,
            model = form.model.data,
            serialNumber = form.serialNumber.data,
            condition = form.condition.data,
            notes = form.notes.data,
            cost = form.cost.data
            )
        db.session.add(newItem)
        db.session.commit()
        return redirect(url_for('invoice'))
    return render_template('invoice/addItems.html', form=form, invoice1=invoice1)

form.py

from flask_wtf import Form
from wtforms import validators, IntegerField, StringField, TextAreaField, HiddenField
from wtforms.ext.sqlalchemy.fields import QuerySelectField
from item.models import Category, Brand


class BrandForm(Form):
    name = StringField('Enter Brand', [validators.Required(), validators.Length(max=80)])

class CategoryForm(Form):
    name = StringField('Enter Category', [validators.Required(), validators.Length(max=80)])

def categories():
    return Category.query.order_by(Category.name).all()

def brands():
    return Brand.query.order_by(Brand.name).all()

class ItemForm(Form):
    #invoice = IntegerField('Invoice Number')
    quantity = IntegerField('Quantity', [validators.Required()])
    category = QuerySelectField('Category', [validators.Required()], query_factory=categories, allow_blank=True, blank_text=u'-- please choose --')
    brand = QuerySelectField('Brand', [validators.Required()], query_factory=brands, allow_blank=True, blank_text=u'-- please choose --')
    model = StringField('Model')
    serialNumber = StringField('Serial Number')
    condition = StringField('Condition')
    notes = TextAreaField('Notes')
    cost = IntegerField('Cost', default=0)

addItems.html

{% extends "base.html" %}

{% block title %}Invoice{% endblock %}

{% block content %}

<div class="row">

    <div class="col-md-offset-3 col-md-6">

        <h3>Add Items to Invoice</h3>

        {{ invoice1.id }}

        {% from "_formhelpers.html" import render_field %}

        <form method="POST" action="{{ url_for('addItem') }}" role="form">


            {{ form.hidden_tag() }}


            {{ render_field(form.quantity, class ='form-control') }}
            {{ render_field(form.category, class ='form-control') }}
            {{ render_field(form.brand, class ='form-control') }}
            {{ render_field(form.model, class ='form-control') }}
            {{ render_field(form.serialNumber, class ='form-control') }}
            {{ render_field(form.condition, class ='form-control') }}
            {{ render_field(form.notes, class ='form-control') }}
            {{ render_field(form.cost, class ='form-control') }}


            <button type="submit" class="btn btn-default">Add Item</button>

        </form>
    </div>

</div>

{% endblock %}

我可以通过shell将记录成功保存到数据库中。

非常感谢任何帮助。

谢谢!

  • 的Izzy

0 个答案:

没有答案