Method Not Allowed flask error 405

前端 未结 4 1232
我寻月下人不归
我寻月下人不归 2020-11-29 07:25

I am developing a flask registration form, and I receive an error:

error 405 method not found.

Code:

import os
# Flask
from         


        
相关标签:
4条回答
  • 2020-11-29 07:43

    Example of a flask app using wsgi with JQuery, Ajax and json:

    activecalls.py

    from flask import Flask, jsonify
    
    application = Flask(__name__, static_url_path='')
    
    @application.route('/')
    def activecalls():
        return application.send_static_file('activecalls/active_calls_map.html')
    
    @application.route('/_getData', methods=['GET', 'POST'])
    def getData():
        #hit the data, package it, put it into json.
        #ajax would have to hit this every so often to get latest data.
        arr = {}
        arr["blah"] = []
        arr["blah"].append("stuff");
    
        return jsonify(response=arr)
    
    
    if __name__ == '__main__':
        application.run()
    

    Javascript json, /static/activecalls/active_calls_map.html:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
    </script>
    
    <script>
    $.ajax({
        //url : "http://dev.consumerunited.com/wsgi/activecalls.py/_getData",
        url : "activecalls.py/_getData",
        type: "POST",
        data : formData,
        datatype : "jsonp",
        success: function(data, textStatus, jqXHR)
        {
            //data - response from server
             alert("'" + data.response.blah + "'");
        },
        error: function (jqXHR, textStatus, errorThrown)
        {
             alert("error: " + errorThrown);
    
        }
    });
    </script>
    

    When you run this. The alert box prints: "stuff".

    0 讨论(0)
  • 2020-11-29 07:44

    Just for people reading on it now. You have to render the /registrazione first, befor you can access the form data. Just write.

    @app.route("/registrazione")
        def render_registrazione() -> "html":
            return render_template("registrazione.html")
    

    before you define def registration(). Sequence is key. You can't access data before the even are available. This is my understanding of the problem.

    0 讨论(0)
  • 2020-11-29 07:55

    change name of method registration

    @app.route('/registrazione', methods=['POST']) 
    def registrazione(): 
        if request.method == 'POST':
           username= request.form.username.data
           return render_template('registration.html', username=username)
       else :
           return render_template('registration.html')
    
    0 讨论(0)
  • 2020-11-29 08:05

    This is because you only allow POST requests when defining your route.

    When you visit /registrazione in your browser, it will do a GET request first. Only once you submit the form your browser will do a POST. So for a self-submitting form like yours, you need to handle both.

    Using

    @app.route('/registrazione', methods=['GET', 'POST']) 
    

    should work.

    0 讨论(0)
提交回复
热议问题