我创建了以下登录表单和随附的模板:
main.rs
#[macro_use]
extern crate nickel;
extern crate mustache;
extern crate rustc_serialize;
use std::collections::HashMap;
use nickel::{Nickel, MediaType, HttpRouter};
use nickel::status::StatusCode;
fn main() {
let mut server = Nickel::new();
let mut router = Nickel::router();
router.get("/", middleware!(|request, mut response| {
response.set(StatusCode::Ok);
response.set(MediaType::Html);
return response.send_file("assets/login.tpl");
}));
router.post("/login", middleware!(|request, mut response| {
response.set(StatusCode::Ok);
response.set(MediaType::Html);
let mut data: HashMap<&str, &str> = HashMap::new();
data.insert("error", "hello");
return response.render("assets/login.tpl", &data);
}));
server.utilize(router);
server.listen("127.0.0.1:6767");
}
资产/登录.tpl
<html lang="en">
<head>
<meta charset="utf8"/>
</head>
<body>
<h1>Login</h1>
<form method="post" action="login">
<label for="email">Email</label>
<input type="email" name="email"/>
<br/>
<label for="password">Password</label>
<input type="password" name="password"/>
<br/>
<button type="submit">Login</button><br/>
<a href="/register">Register</a>
</form>
{{error}}
</body>
</html>
当我第一次提交表单时,我看到了“hello”消息。如果我再次提交表单,我会看到“未找到”。
我无法弄清楚问题出在哪里。