champ_ 2023-08-12 18:16 采纳率: 88.5%
浏览 10
已结题

html,修改<form action=的参数就会报错

在编辑一个html文件时,修改一下内容:


```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="https://localhost/5000" method="post">  <!--这里一改url_for 就报错 ?-->
        <p>姓名:<input type="text" name="姓名"></p>
        <p>年龄:<input type="text" name="年龄"></p>
        <p>性别:<input type="text" name="性别"></p>
        <p>地址:<input type="text" name="地址"></p>
        <p><input type="submit" value="提交"></p>
    </form>
</body>
</html>

app.py中的代码为:

```python
#表单提交
@app.route('/test/register')
def test1():
    return render_template('test/register.html')


#接受表单路由 指定methods
@app.route('/result',methods=['POST','GET'])
def test2():
    if request.method=='POST':
        result=request.form
        return render_template('test/result.html',result=result)

if __name__ == '__main__':
    app.run(debug=True)

把action属性后面的url改成:url_for(’result‘) 就会报错 ,报错内容为:

werkzeug.routing.exceptions.BuildError: Could not build url for endpoint '/result'. Did you mean 'test1' instead?

  • 写回答

2条回答 默认 最新

  • 小小小小人水豚 2023-08-12 18:54
    关注

    url_for函数正确的用法是:url_for('视图函数名字') ,所以html文件应该改成:

    <form action="{{ url_for('test2') }}" method="post">
    

    有帮助的话,请点采纳该答案~

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月22日
  • 已采纳回答 8月14日
  • 创建了问题 8月12日