flask中缺少3个必需的位置参数错误,即使在我指定它们之后也是如此

2024-10-01 22:43:01 发布

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

所以我从用户那里获取了3个输入,都是文件路径,然后将它们输入到一个函数中,这个函数可以正常工作。但我每次都会犯上面提到的错误,我哪里出了错

app = Flask(__name__)

@app.route('/info', methods = ["POST", "GET"])
def infotaking():
    if request.method == "POST":
        dp1 = request.form["dp"]
        mf1 = request.form["mf"]
        tess1 = request.form["tess"]
        #return redirect(url_for("user", pdflocation=dp))
        return redirect(url_for("aadharmask", pdflocation=dp1, masklocation=mf1, tessLocation=tess1))
    else:
        return render_template("infotaking.html")


@app.route('/masking')
def aadharmask(pdflocation, masklocation,tessLocation):

正在使用infotaking.html获取dp、mf和tess

{% block title %} Input file paths {% endblock %}

{% block content %}
<form action = "#" method = "POST">
    <p>datapath:</p>
    <p><input type ="text" name ="dp" /></p>
    <p>masked files saving path:</p>
    <p><input type ="text" name ="mf" /></p>
    <p>tesseract ocr location</p>
    <p><input type ="text" name ="tess" /></p>
    <p><input type ="submit" value ="submit" /></p>
</form>
{% endblock %}

编辑 使用@Parzival的解决方案后,会弹出一个新的错误,路径被读取错误,即它们应该是什么

  • pdflocation:D:/dataset/pdf
  • 掩码位置:D:/dataset/masked
  • tesseract位置:D:/tesseract/tesseract.exe

他们是什么

  • pdflocation:D

  • masklocation:dataset

  • tesseract位置:pdf/D:/dataset/maskedD:/tesseract/tesseract.exe


Tags: nameformappinputrequesttype错误post
1条回答
网友
1楼 · 发布于 2024-10-01 22:43:01

尝试:

@app.route('/masking/<path:pdflocation>/<path:masklocation>/<path:tesslocation>')
def aadharmask(pdflocation, masklocation,tessLocation):

如有必要,还可以指定数据类型:

<int:pdflocation>

问题来自第二个应用程序路由,您没有指定如何生成用于屏蔽的URL

@app.route('/masking') # No parameters are specified!
def aadharmask(pdflocation, masklocation,tessLocation):

函数aadharmask需要url提供三个参数(pdflocation、masklocation、tessLocation),但没有提供任何参数。您需要指定如何使用给定参数创建URL-除非指定,否则flask无法为具有参数的函数生成URL。 See Flask documentation

编辑

似乎参数中有/字符,flask将其解释为另一条路由 尝试使用

在URL中指定path类型,例如:

<path:pdflocation>

新选项

尝试:

<form action = "/masking" method = "POST">
    <p>datapath:</p>
    <p><input type ="text" name ="dp" /></p>
    <p>masked files saving path:</p>
    <p><input type ="text" name ="mf" /></p>
    <p>tesseract ocr location</p>
    <p><input type ="text" name ="tess" /></p>
    <p><input type ="submit" value ="submit" /></p>
</form>
@app.route('/masking')
def aadharmask():
    if request.method == "POST":
        dp1 = request.form["dp"]
        mf1 = request.form["mf"]
        tess1 = request.form["tess"]
        # process files if necessary

相关问题 更多 >

    热门问题