我在弄清楚我的函数editMenuItem()
从哪里获取未知关键字参数restaurant_id
时遇到麻烦。我已经搜索了类似的问题,但找不到解决方案。如果有人有任何建议,我将不胜感激。
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
TypeError: editMenuItem() got an unexpected keyword argument 'restaurant_id'
editMenuItem()
### Edit a menu item. ###
@app.route('/restaurant/<int:restaurant_id>/menu/<int:menuitem_id>/edit/',
methods=['GET', 'POST'])
def editMenuItem(menuitem_id):
menuitem = session.query(MenuItem).filter_by(id=menuitem_id).one()
if request.method == 'POST':
if request.form['newname']:
menuitem.name = request.form['newname']
if request.form['newdescription']:
menuitem.description = request.form['newdescription']
if request.form['newprice']:
menuitem.description = request.form['newprice']
session.add(menuitem)
session.commit()
return redirect(url_for('showMenu', restaurant_id=menuitem.restaurant_id))
else:
return render_template('editMenuItem.html', menuitem=menuitem)
Relevant html
{% extends "base.html" %}
{% block content %}
<div class="row">
<div class="six columns">
<form action="{{ url_for('editMenuItem', menuitem_id = menuitem.id) }}" method="post">
<div class="six columns">
<div class="six columns">
Menu Item ID : {{ menuitem.id }}
<br>
Menu Item Name : {{ menuitem.name }}
<br>
Menu Item Description : {{ menuitem.description }}
<br>
Menu ITem Price : {{ menuitem.price }}
<br>
</div>
<br>
<div class="six columns">
<div class="six columns">
<label for="newItemName">New item name</label>
<input type="text" id="newname" class="form" name="newname" placeholder="Enter new name here">
<br>
</div>
<div class="six columns">
<label for="newItemDescription">New item Description</label>
<input type="text" id="newdescription" class="form" name="newdescription" placeholder="Enter new description here">
<br>
</div>
<div class="six columns">
<label for="newItemPrice">New item Price</label>
<input type="text" id="newprice" class="form" name="newprice" placeholder="Enter new price here">
<br>
</div>
</div>
<div class="six columns">
<button type="submit" class="button">Edit</button>
<a href="{{ url_for('showMenu') }}" class="button">Cancel</a>
<br>
</div>
</div>
</form>
</div>
</div>
{% endblock %}
我不确定此关键字参数来自何处,因为不必给出
restaurant_id
即可使我的函数正常工作。我在这里做错了什么?谢谢。 最佳答案
感谢Wombatz提示,我能够通过更改editMenuItem
函数并更改必要的模板来解决此问题。
更新功能:
### Edit a menu item. ###
@app.route('/restaurant/<int:restaurant_id>/menu/<int:menuitem_id>/edit/',
methods=['GET', 'POST'])
def editMenuItem(menuitem_id, restaurant_id):
menuitem = session.query(MenuItem).filter_by(id=menuitem_id).one()
restaurant = session.query(Restaurant).filter_by(id=restaurant_id).one()
if request.method == 'POST':
if request.form['newname']:
menuitem.name = request.form['newname']
if request.form['newdescription']:
menuitem.description = request.form['newdescription']
if request.form['newprice']:
menuitem.description = request.form['newprice']
session.add(menuitem)
session.commit()
return redirect(url_for('showMenu', restaurant_id=menuitem.restaurant_id))
else:
return render_template('editMenuItem.html', menuitem=menuitem, restaurant=restaurant)
更新的模板:
<form action="{{ url_for('editMenuItem', menuitem_id = menuitem.id, restaurant_id = restaurant.id) }}" method="post">
<a href="{{ url_for('showMenu', restaurant_id=menuitem.restaurant_id) }}" class="button">Cancel</a>
我是刚接触此网站的人,所以不确定是否可以“支持”您的评论Wombatz,但感谢您的提示。