404找不到“找不到请求的URL”

时间:2017-11-10 20:47:19

标签: python html css flask

我正在尝试在sida2中创建一个按钮带我到结果并从输入表单发布名为MJ的信息,但是我收到错误404在服务器上找不到请求的URL,我不明白为什么。这是html部分:

<form action="/sida2/resultat.html" method="POST">
<input title="" placeholder="MJ/kq foder" type="text" name="MJ" required>
<br>
<button type="submit">Submit</button>
</form>

这是python部分:

from flask import Flask, render_template, request

app=Flask(__name__)

@app.route('/')
def home():
    return(render_template("hemsida.html")) 


@app.route('/sida2/', methods=['POST', 'GET'])
def sida2():
    return(render_template("andrasidan.html"))


@app.route('/sida2/resultat', methods=['POST'])
def resultat():    
    if request.method=='POST':       
        mj= request.form["MJ"]



    return(render_template("resultat.html"))

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

我认为这是显而易见的我不知道但我似乎无法找到它。

1 个答案:

答案 0 :(得分:2)

使用url_for生成Flask视图的网址。您想要的视图是resultat

<form action="{{ url_for('resultat') }}" method="POST">

这将为您的resultat()功能生成相应的网址:

@app.route('/sida2/resultat', methods=['POST'])
def resultat():

您的表单操作中当前拥有的网址(/sida2/resultat.html)将无效,因为您的代码会绑定到网址/sida2/resultat

要快速了解使用url_for而不是硬编码网址的好处,请查看the Flask quickstart section on the topic

相关问题