简体   繁体   中英

405 when submitting form to Flask app

I'm trying to create a webpage in html in which a user have to input a url into a textfield.

<form name="submitURL" method ="post">
<input type="text" name="url_target">
<button type="submit">submit</button>

When the user press the submit button a python script should intercept the data inserted by the user and pass it to another function inside this script.

Python script:

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route('/')
def indhtml() :
   return render_template('index.html')

@app.route('/index.html', methods = ['POST'])
def result():
    result = request.form('url_target')
    link = result.link
    print (link)
    return

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

the first render is fine and i can see the index.html page, but when i insert something in the textfield appears the error HTTP 405.

Do you have any suggestion for that? :( Thanks!

您的函数result()返回任何内容。

You forgot action="{{ url_for('result') }}" in your <form> tag, so you are submitting the form to the same page, which is not accepting POST requests.

Additionally, request.form is dict-like, so you need [] and not () to access it. link = result.link is going to fail too because there is no way that link attribute exists on the string you get from request.form ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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