【问题标题】:405 Method Not Allowed: The method is not allowed for the requested URL405 Method Not Allowed:请求的 URL 不允许该方法
【发布时间】:2021-02-01 07:58:32
【问题描述】:

我一直在开发一个系统,但我最近无法调试为什么会出现此错误 不允许的方法 请求的 URL 不允许该方法。 我一直在开发一个系统 我认为问题与不允许发布功能并被某些东西打断有关,请帮助我谢谢。 代码:

蟒蛇

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from flask import Flask, render_template, request
app = Flask(__name__)

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

@app.route('/result',methods = ['POST', 'GET'])
def result():
    if request.method == 'POST':
        result = request.form['name']
        print(result)
        return "thank you for filling out this form"

if __name__ == '__main__':
    app.run(debug = True)
file_object = open('transferfile.txt', 'a+')
name = "Gabriel"
age = "12"
gender = "male"
file_object.write(name)
file_object.write(" ")
data = file_object.read(100)
file_object.write(age)
file_object.write(" ")
file_object.write(gender)
file_object.close()

html

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="style.css">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>venuefast</title>
</head>
<body>
    <form class="logo" action="." method="post">fastvenue<br>
        <hr>
        <input type ="text" name="name" placeholder="name">
        <br>
        <button type="submit" value="submit">Submit</button>
    </form> 
</body>
</html>

【问题讨论】:

    标签: python html forms flask


    【解决方案1】:

    由于学生函数在 index.html 中呈现表单,因此只有学生函数接受 POST 请求才有意义:

    from flask import Flask, render_template, request
    app = Flask(__name__)
    
    @app.route('/',methods = ['POST', 'GET'])
    def student():
        if request.method == 'POST':
            name = request.form['name']
            return f"thank you for filling out this form {name}!"
        return render_template('index.html')
    
    
    @app.route('/result')
    def result():
        return 'this function does nothing yet'
    
    if __name__ == '__main__':
        app.run(debug = True)
    
    

    另一种选择,如果您打算将 student 用于其他用途,您可以让 result 函数呈现 index.html 并使其接受 POST 请求:

    
    from flask import Flask, render_template, request
    app = Flask(__name__)
    
    @app.route('/')
    def student():
        return 'this function does nothing yet'
    
    
    @app.route('/result',methods = ['POST', 'GET'])
    def result():
        if request.method == 'POST':
            name = request.form['name']
            return f"thank you for filling out this form {name}!"
        return render_template('index.html')
    
    if __name__ == '__main__':
        app.run(debug = True)
    
    

    【讨论】:

      【解决方案2】:

      在 HTML 中试试这个

      <html lang="en">
      <head>
          <link rel="stylesheet" href="style.css">
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <title>venuefast</title>
      </head>
      <body>
          <form class="logo" action="/result" method="POST">
              <p>fastvenue</p>
              <br>
              <hr>
              <input type ="text" name="name" placeholder="name">
              <br>
              <button type="submit" value="submit">Submit</button>
          </form> 
      </body>
      </html>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-27
        • 1970-01-01
        • 2012-06-22
        • 2016-01-22
        • 2023-03-12
        • 2014-08-31
        • 2021-03-15
        相关资源
        最近更新 更多