简体   繁体   中英

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. 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)

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.

The Error indicates you're doing a "GET" operation, on the url '/post'.

web.py takes the URL and looks it up in the urls list, and identifies the url '/post' is handled by the class "PostRegistration".

So, web.py calls the GET method on class PostRegistration, which doesn't exist, or, as web.py says "Method Not Allowed".

To get past that, either use a POST operation (as @Barmar suggests) or, rename PostRegistration.POST(self) to PostRegistration.GET(self).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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