Merge pull request #698 from liunaijia/patch-5

Add debug log for template execution
This commit is contained in:
astaxie
2016-08-22 10:37:40 +08:00
committed by GitHub

View File

@@ -48,7 +48,7 @@ http包里面有一个很简单的方式就可以获取我们在前面web的
fmt.Println("method:", r.Method) //获取请求的方法
if r.Method == "GET" {
t, _ := template.ParseFiles("login.gtpl")
t.Execute(w, nil)
log.Println(t.Execute(w, nil))
} else {
//请求的是登陆数据,那么执行登陆的逻辑判断
fmt.Println("username:", r.Form["username"])
@@ -74,6 +74,8 @@ login函数中我们根据`r.Method`来判断是显示登录界面还是处理
![](images/4.1.login.png?raw=true)
如果你看到一个空页面,可能是你写的 login.gtpl 文件中有错误,请根据控制台中的日志进行修复。
图4.1 用户登录界面
我们输入用户名和密码之后发现在服务器端是不会打印出来任何输出的为什么呢默认情况下Handler里面是不会自动解析form的必须显式的调用`r.ParseForm()`后,你才能对这个表单数据进行操作。我们修改一下代码,在`fmt.Println("username:", r.Form["username"])`之前加一行`r.ParseForm()`,重新编译,再次测试输入递交,现在是不是在服务器端有输出你的输入的用户名和密码了。