我有一个Python控制器文件,一个表单和一个javascript文件,在单击表单上的提交按钮时执行ajax代码。表单正确显示。当我在表单中输入数据时,ajax代码触发并执行驻留在controller.py文件中的PostRegistration类中的代码。执行代码时,我将“内部类PostRegistration”打印到控制台,然后通过使用代码data = web.input()检索表单数据。当我将数据内容打印到控制台时,我得到“data =”。我希望看到表单名称和值。然后我执行一行代码print(data.username),但这会生成“KeyError:'username'”。我的假设是web.input()命令没有检索表单字段名称,但我不确定。如何检索输入到表单输入元素的数据?非常感谢任何帮助。
以下是涉及的文件:
controller.py
import web
from Models import RegisterModel
urls = (
'/', 'Home',
'/register', 'Register',
'/postregistration', '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):
print("inside class PostRegistration")
data = web.input()
print("data=",data)
print(data.username)
return data
if __name__ == "__main__":
app.run()
register.html
<div class="container">
<h2>Register Account</h2>
<br /><br />
<form id="register-form">
<div class="form-group label-static is-empty">
<label for="username" class="control-label">Username</label>
<input id="username" class="form-control" type="text" placeholder="Choose a username" />
</div>
<div class="form-group label-static is-empty">
<label for="fullname" class="control-label">Full Name</label>
<input id="fullname" 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</label>
<input id="email" class="form-control" type="text" placeholder="Enter your email address" />
</div>
<div class="form-group label-static is-empty">
<label for="password" class="control-label">Password</label>
<input id="password" class="form-control" type="password" placeholder="Enter your password" />
</div>
<button type="submit" class="btn btn-info waves-effect ripple">Submit </button>
</form>
</div>
scripty.js
$(document).ready(function(){
console.log("loaded");
$(document).on("submit", "#register-form", function(e){
e.preventDefault();
var form = $('#register-form').serialize();
$.ajax({
url: "/postregistration",
type: "POST",
data: form,
success: function(response){
console.log(response);
}
});
});
});
mainlayout.html
$def with (page)
$var css: static/css/bootstrap3.3.7.css static/css/material.css static/css/ripple.min.css
$var js: static/js/jquery-3.2.1.js static/js/bootstrap3.3.7.js static/js/material.js static/js/ripple.min.js static/js/scripty.js
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CodeWizard</title>
$if self.css:
$for style in self.css.split():
<link rel="stylesheet" href="$style" />
</head>
<body>
<div id="app">
<div class="navbar navbar-info navbar-fixed-top">
<div class="navbar-header">
<a class="navbar-brand">CodeWizard</a>
</div>
<ul class="nav navbar-nav">
<li>
<a class="waves-effect" href="/">Home Feed<div class="ripple-container"></div></a>
</li>
<li>
<a class="waves-effect" href="/discover">Discover<div class="ripple-container"></div></a>
</li>
<li>
<a class="waves-effect" href="/profile">Profile<div class="ripple-container"></div></a>
</li>
<li>
<a class="waves-effect" href="/settings">Settings<div class="ripple-container"></div></a>
</li>
</ul>
<div class="pull-right">
<a href="/register" class="btn btn-raised btn-default waves-effect">Register</a>
</div>
</div>
<br /><br />
$:page
</div>
$if self.js:
$for script in self.js.split():
<script src="$script"></script>
</body>
</html>