From 8ad98ae31094c9d9c2aec76ee133ef37cf7f4970 Mon Sep 17 00:00:00 2001 From: Naijia Liu Date: Sun, 21 Aug 2016 14:53:20 +0800 Subject: [PATCH] Add debug log for template execution In order to display errors such as wrong html syntax and bad html structure in "login.gtpl", I'd like to suggest to add log information to help people release errors in the template file. Currently if I'm trying to render a bad template file I'll see a blank page without any information. --- zh/04.1.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/zh/04.1.md b/zh/04.1.md index 9fa3b0ea..4f7217c6 100644 --- a/zh/04.1.md +++ b/zh/04.1.md @@ -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()`,重新编译,再次测试输入递交,现在是不是在服务器端有输出你的输入的用户名和密码了。