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

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

问题描述

  error 405找不到方法。 

代码:

  import os 
#Flask $ b $ from flask flask Flask,request,session,g,redirect,url_for,abort,\
render_template,flash,Markup,send_from_directory,escape $从werkzeug进口的b $ b secure_filename
$ b $来自cultura进口应用程序

#我的应用程序
from包含进口用户

@ 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 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邮件:< / 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'>密码*:< / 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 ,我收到错误。我在做什么错了?

解决方案

这是因为您在定义路由时只允许POST请求。
$ b

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



使用

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

应该有效。


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

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>

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

解决方案

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.

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

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