类型错误:()得到一个意外的关键字参数

时间:2017-02-16 19:01:34

标签: python html templates flask jinja2

我无法确定我的函数 UIView.transition(with: view, duration: 0.5,options: .transitionCurlUp,animations: { () -> Void in self.schedule.reloadData() self.formatter.dateFormat = "yyyy-MM-dd" let dayOfWeek = self.week[self.getDayOfWeek(self.currentDate)!-1] var forReturn = dayOfWeek.getCourseNumber() if forReturn>3{ forReturn = forReturn + 1 self.refreshButton.isHidden = false self.noClasses.isHidden = true }else{ self.refreshButton.isHidden = true self.noClasses.isHidden = false } self.changeWeek() let startDate = self.formatter.string(from: self.startDateBase) if self.currentDate == startDate{ self.other1.text = "Today's Schedule" }else{ self.formatter.dateFormat = "yyyy-MM-dd" self.currentDate = self.formatter.string(from: self.date) self.other1.text = dayNames[self.getDayOfWeek(self.currentDate)!-1] + " Schedule" } }, completion: nil ); 从哪里获取未知关键字参数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 %} 不需要为了让我的函数工作而需要。我在这做错了什么?谢谢。

1 个答案:

答案 0 :(得分:1)

感谢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,但感谢提示。