Python Flas中route函数的参数问题

2024-10-03 21:28:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在我的tabort函数中有两个参数,但它一直说缺少一个必需的位置参数。我不知道该怎么办我有两个在那里,但它仍然说一个是失踪的。有人能帮我吗?你知道吗

@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'


Tags: inpyappflasklibpackageslocalline
2条回答

在您的代码中,您构建的URL如下所示:

@app.route('/tabort/<int:id>', methods=['GET', 'POST'])
def tabort(id,username):

URL只使用了一个参数,但是方法需要两个参数,这就错了。你知道吗

为了向方法发送两个参数,还需要向URL发送两个参数,如下所示:

@app.route('/tabort/<int:id>/<string:username', methods=['GET', 'POST'])
def tabort(id,username):

我希望这有帮助。你知道吗

@app.route('/tabort/<int:id>', methods=['GET', 'POST'])
def tabort(id,username):

您的路径中有<int: id>。因此flask将解析路径中的一个id整数,并将其传递给函数。但是你的函数要寻找两个参数,id和username,而路由器只传递一个。要么从arg列表中删除username,要么将<username>添加到某个路径。你知道吗

相关问题 更多 >