[英]python flask avoid function arguments which appear in route, but unnecessary
[英]Problem with arguments in route function in Python Flask
我的 tabort 函數中需要有兩個參數,但它一直說缺少 1 個必需的位置參數。 我不知道如何處理它,我在那里有兩個,但它仍然說缺少一個。 有人可以幫我解決這個問題嗎?
@app.route('/tabort/<int:id>', methods=['GET', 'POST'])
def tabort(id,username):
"""
Delete the item in the database that matches the specified
id in the URL
"""
qry = db_session.query(Matrial).filter(
Matrial.id==id)
matrial = qry.first()
if matrial:
form = MatrialForm(formdata=request.form, obj=matrial)
if request.method == 'POST' and form.validate():
# delete the item from the database
db_session.delete(matrial)
db_session.commit()
flash('Artikel togs bort!')
return redirect(url_for("user_home", username=username))
return render_template('tabort_artikel.html', form=form)
else:
return 'Något gick fel #{id}'.format(id=id)
Traceback (most recent call last)
File "C:\Users\xzenon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\xzenon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\xzenon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\xzenon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\xzenon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\xzenon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\xzenon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\xzenon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\xzenon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\xzenon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
TypeError: tabort() missing 1 required positional argument: 'username'
@app.route('/tabort/<int:id>', methods=['GET', 'POST'])
def tabort(id,username):
您的路徑中有<int: id>
。 因此,flask 將從路徑中解析一個 id 整數並將其傳遞給您的函數。 但是你的函數正在尋找兩個參數,id 和 username,而路由器只傳遞一個。 從您的參數列表中刪除username
,或將<username>
添加到路徑中的某處。
在您的代碼中,您正在構建如下 URL:
@app.route('/tabort/<int:id>', methods=['GET', 'POST'])
def tabort(id,username):
URL 只接受一個參數,但該方法需要兩個參數,你就錯了。
為了向方法發送兩個參數,您還需要向 URL 發送 2 個參數,如下所示:
@app.route('/tabort/<int:id>/<string:username', methods=['GET', 'POST'])
def tabort(id,username):
我希望這有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.