简体   繁体   English

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

[英]404 not found “requested URL was not found”

I'm trying to make a button in sida2 take me to resultat and post the information named MJ from the input form but I get an error 404 The requested URL was not found on the server and I don't understand why. 我试图用sida2中的按钮将我带到resultat并从输入表单中发布名为MJ的信息,但出现错误404在服务器上找不到请求的URL,我不明白为什么。 This is the html part: 这是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>

And this is the python part: 这是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)

I assume it's something obvious I'm missing but I just can't seem to find it. 我认为这很明显我很想念,但我似乎找不到。

Use url_for to generate URLs to Flask views. 使用url_for生成Flask视图的URL。 The view you want is resultat . 您想要的视图是resultat

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

This will generate the appropriate URL for your resultat() function: 这将为您的resultat()函数生成适当的URL:

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

The URL you currently have in your form action ( /sida2/resultat.html ) will not work as your code binds to the URL /sida2/resultat instead. 您当前在表单操作中拥有的URL( /sida2/resultat.html )将不起作用,因为您的代码将绑定到URL /sida2/resultat

For a quick overview of the benefits of why you should use url_for over hardcoding your URLs, check out the Flask quickstart section on the topic . 要快速概述为什么应使用url_for不是对URL进行硬编码的好处,请查看主题上的Flask快速入门部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM