Flask和Bootstrap中的发布请求未将数据添加到数据库中

时间:2018-06-25 15:53:28

标签: python sqlite flask sqlalchemy bootstrap-4

我试图使用Bootstrap中的表单,并使用SQLAlchemy和Flask将数据字段添加到数据库中,但是当我尝试提交它时,它不会重定向到我想要的页面。

这是我的project_cat.py文件:

   @app.route('/category/<int:category_id>/items/new/', methods=['GET', 'POST'])
   def newMenuItem(category_id):
if 'username' not in login_session:
    return redirect('/login')
category = session.query(Category).filter_by(id=category_id).one()
if login_session['user_id'] != category.user_id:
    return "<script>function myFunction() {alert('You are not authorized to add menu items to this restaurant. Please create your own restaurant in order to add items.');}</script><body onload='myFunction()'>"
    if request.method == 'POST':
        NewItem = Item(name=request.form['name'], description=request.form['description'], price=request.form['price'], category_id = category_id, user_id=category.user_id)
        session.add(NewItem)
        session.commit()
        flash('New Menu %s Item Successfully Created' % (NewItem.name))
        return redirect(url_for('showMenu', category_id=category_id))
else:
    return render_template('newmenuitem.html', category_id=category_id)

newmenuitem.html:

    <div class="row">
    <div class="col-md-6 col-md-offset-1 padding-top">
        <form action="#" method = "POST">
            <div class="form-group">
                <label for="name">Name:</label>
                <input type ="text" maxlength="50" class="form-control" name="name" placeholder="Name of the Item">

                <label for="description">Description:</label>
                <textarea class="form-control" maxlength="250" rows="3" name="description" placeholder="Description of the Item"></textarea>

                <label for="price">Price:</label>
                <div class="input-group">
                    <div class="input-group-addon">$</div>
                    <input type ="text" class="form-control" maxlength="10" name="price" placeholder="Price in $">
                </div>
                <div>
                <button type="submit" class="btn btn-default" id="submit" type="submit">
                <span class="glyphicon glyphicon-glass" aria-hidden="true"></span>Create</button>
                </div>
            </div>
        </form>
    </div>

在操作中添加url_for后,Evan会执行相同的操作。

数据库文件:

    class Item(Base):
__tablename__ = 'items'

name = Column(String(80), nullable=False)
id = Column(Integer, primary_key=True)
date = Column(DateTime, nullable =  False)
description = Column(String(250))
category_id = Column(Integer, ForeignKey('category.id'))
category = relationship(Category)
user_id = Column(Integer, ForeignKey('user.id'))
user = relationship(User)

1 个答案:

答案 0 :(得分:0)

该错误是由于您在project_cat.py中编写if else语句的方式引起的

如果我正确理解了您的问题,那么这就是您想要的

@app.route('/category/<int:category_id>/items/new/', methods=['GET', 'POST'])
def newMenuItem(category_id):
    if 'username' not in login_session:
        return redirect('/login')
    category = session.query(Category).filter_by(id=category_id).one()
    if login_session['user_id'] != category.user_id:
        return "<script>function myFunction() {alert('You are not authorized to add menu items to this restaurant. Please create your own restaurant in order to add items.');}</script><body onload='myFunction()'>"
    if request.method == 'POST':
        NewItem = Item(name=request.form['name'], description=request.form['description'], price=request.form['price'], category_id = category_id, user_id=category.user_id)
        session.add(NewItem)
        session.commit()
        flash('New Menu %s Item Successfully Created' % (NewItem.name))
        return redirect(url_for('showMenu', category_id=category_id))
    else:
        return render_template('newmenuitem.html', category_id=category_id)