简体   繁体   English

405方法不允许创建用户时发帖请求时出错

[英]405 Method Not Allowed Error in post request while creating user

I'm making a user registration form in which I have made a post request to get the user input data in a print out form but 405 Method Not allowed error causing the problem. 我正在制作一个用户注册表格,在该表格中,我已经发出了发布请求,以打印出表格的形式获取用户输入的数据,但是405方法不允许出现错误,从而导致了问题。 I'm a newbie so that's why I couldn't figure out what's going wrong. 我是新手,所以这就是为什么我不知道出了什么问题。

CONTROLLER CLASS: 控制器类:

import web
from Models import RegisterModel

urls = (
    "/", "Home",
    "/register", "Register",
    "/post", "PostRegistration",
)
render = web.template.render("Views/Templates", base="MainLayout")
app = web.application(urls, globals())


# Classes/routes

class Home:
    def GET(self):
        return render.Home()


class Register:
    def GET(self):
        return render.Register()


class PostRegistration:
    def POST(self):
        data = web.input()
        reg_model = RegisterModel.RegisterModelCls()
        reg_model.insert_user(data)
        return data.username


if __name__ == "__main__":
    app.run()

Registration form: 报名表格:

<div class="container">
<h2>Register Account</h2>
<br /><br />
<form>

<div class="form-group label-static is-empty">
    <label for="username" class="control-label">Username</label>
    <input id="username" name="username" class="form-control" 
    type="text" placeholder="Choose a username" />
</div>
<div class="form-group label-static is-empty">
    <label for="display_name" class="control-label">Full Name</label>
    <input id="display_name" name="name" class="form-control" 
    type="text" placeholder="Enter your full name" />
</div>
<div class="form-group label-static is-empty">
    <label for="email" class="control-label">Email Address</label>
    <input id="email" name="email" class="form-control" type="email" 
    placeholder="Enter your Email" />
</div>
<div class="form-group label-static is-empty">
    <label for="password" class="control-label">Password</label>
    <input id="password" name="password" class="form-control" 
    type="password" placeholder="Make a password" />
</div>

<a type="submit" href="/post"  class="btn btn-info waves-effect" 
>Submit <div class="ripple-container"></div></a>
</form>
</div>

RegistrationModel.py class (which suppose to print the user input) RegistrationModel.py类(假定打印用户输入)

import pymongo
from pymongo import MongoClient


class RegisterModelCls:
    def insert_user(self, data):
        print("data is: " + data)

Error: 错误:

http://0.0.0.0:8080/

127.0.0.1:64395 - - [06/Sep/2019 00:19:16] "HTTP/1.1 GET /post" - 405 
Method Not Allowed

You're mixing GET and POST. 您正在混合GET和POST。

The Error indicates you're doing a "GET" operation, on the url '/post'. 错误表示您正在网址“ / post”上执行“ GET”操作。

web.py takes the URL and looks it up in the urls list, and identifies the url '/post' is handled by the class "PostRegistration". web.py获取URL并在URL列表中查找,并标识URL“ / post”由类“ PostRegistration”处理。

So, web.py calls the GET method on class PostRegistration, which doesn't exist, or, as web.py says "Method Not Allowed". 因此,web.py在类PostRegistration上调用GET方法,该方法不存在,或者如web.py所说的“不允许使用方法”。

To get past that, either use a POST operation (as @Barmar suggests) or, rename PostRegistration.POST(self) to PostRegistration.GET(self). 要克服这一点,请使用POST操作(如@Barmar建议),或者将PostRegistration.POST(self)重命名为PostRegistration.GET(self)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM