方法不允许烧瓶错误 405 [英] Method Not Allowed flask error 405

查看:37
本文介绍了方法不允许烧瓶错误 405的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个烧瓶注册表,我收到一个错误:

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

error 405 method not found.

代码:

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 时,我收到错误消息.我做错了什么?

when I visit localhost:5000/registrazione, I receive the error. What am I doing wrong?

推荐答案

这是因为你在定义路由时只允许 POST 请求.

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

当您在浏览器中访问 /registrazione 时,它会首先执行 GET 请求.只有在您提交表单后,您的浏览器才会执行 POST.因此,对于像您这样的自行提交表单,您需要同时处理两者.

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.

使用

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

应该可以.

这篇关于方法不允许烧瓶错误 405的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆