强人锁男 发表于 2018-9-27 17:43:53

error 405 method not found.

我正在开发一个flask注册表,我收到一个错误:
error 405 method not found.
Code:
import os
# Flask
from flask import Flask, request, session, g, redirect, url_for, abort, \
   render_template, flash, Markup, send_from_directory, escape
from werkzeug import secure_filename

from cultura import app

# My app
from include import User
@app.route('/')
def index():
    return render_template('hello.html')

@app.route('/registrazione', methods=['POST'])
def registration():
    if request.method == 'POST':
      username= request.form.username.data
      return render_template('registration.html', username=username)
    else :
      return render_template('registration.html')
registration.html:
<html>
<head> <title>Form di registrazione </title>
</head>

    <body>
    {{ username }}
    <form id='registration' action='/registrazione' method='post'>
    <fieldset >
    <legend>Registrazione utente</legend>
    <input type='hidden' name='submitted' id='submitted' value='1'/>
    <label for='name' >Nome: </label>
    <input type='text' name='name' id='name' maxlength="50" /> <br>
    <label for='email' >Indirizzo mail:</label>
    <input type='text' name='email' id='email' maxlength="50" />
   <br>
    <label for='username' >UserName*:</label>
    <input type='text' name='username' id='username' maxlength="50" />
   <br>
    <label for='password' >Password*:</label>
    <input type='password' name='password' id='password' maxlength="50" />
    <br>
    <input type='submit' name='Submit' value='Submit' />

    </fieldset>
    </form>
    </body>
    </html>
当我访问localhost:5000/registrazione时,我收到了错误。

德国骨科 发表于 2018-9-27 17:47:27

这是因为在定义路由时只允许POST请求。当您在浏览器中访问/registrazione时,它将首先执行GET请求。只有当你提交了表单,你的浏览器才会做一个 POST。所以对于像你这样的自我提交表单,你需要同时处理这两种情况。
Using@app。route('/registrazione',methods=['GET', 'POST'])应该可以工作。

I_Like_AI 发表于 2018-9-27 17:49:31

使用wsgi和JQuery、Ajax和json的flask应用程序示例:
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>

页: [1]
查看完整版本: error 405 method not found.