我正在制作一个用户注册表单,其中我发出了一个发布请求以在打印表单中获取用户输入数据,但 405 Method Not allowed 错误导致了问题。我是新手,所以这就是为什么我无法弄清楚出了什么问题。
控制器类别:
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()
报名表:
<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 类(假设打印用户输入)
import pymongo
from pymongo import MongoClient
class RegisterModelCls:
def insert_user(self, data):
print("data is: " + data)
错误:
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
您正在混合 GET 和 POST。
错误表明您正在对 url '/post' 执行“GET”操作。
web.py 获取 URL 并在 url 列表中查找它,并识别 url '/post' 由类“PostRegistration”处理。
因此,web.py 在类 PostRegistration 上调用 GET 方法,该方法不存在,或者,如 web.py 所说“不允许使用该方法”。
要解决这个问题,可以使用 POST 操作(如 @Barmar 建议),或者将 PostRegistration.POST(self) 重命名为 PostRegistration.GET(self)。
有人可以帮助解决这个错误吗?在搜索/尝试几个小时后我仍然遇到这个问题。