英文:
GO: How to POST data into datastore?
问题
1)通过模板方法渲染了一个登录页面。
例如:这是index.html
{{ define "title" }}Guestbook{{ end }}
{{ define "content" }}
<form action="/login" method="post">
<div><label>用户名 : </label><input name="username" type="text" /></div>
<div><label>密码 : </label><input name="password" type="password" /></div>
<div><input type="submit" value="登录"></div>
</form>
{{ end }}
-
hello.go文件:
package main
import (
"fmt"
"html/template"
"net/http"
)var index = template.Must(template.ParseFiles(
"templates/base.html",
"templates/index.html",
))
type UserLogin struct{
UserName string
PassWord string
}func handler(w http.ResponseWriter, r *http.Request) {
index.Execute(w, nil)
}/*func login(w http.ResponseWriter, r http.Request) {
remPartOfURL := r.URL.Path[len("/login/"):]
if r.Method == "POST" {
fmt.Fprintf(w, "after",r.FormValue("username"))
}
fmt.Fprintf(w, "Hello %s!", remPartOfURL)
}/
func login(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "登录 : ", "\n")
remPartOfURL := r.URL.Path[len("/login/"):]
if r.Method == "POST" {
g := UserLogin{
UserName: r.FormValue("username"),
PassWord: r.FormValue("password"),
}
fmt.Fprint(w, "&g : ", &g,"\n")
}
fmt.Fprintf(w, "Hello %s!", remPartOfURL)
}func init() {
http.HandleFunc("/", handler)
http.HandleFunc("/login/", login)
}
问题:点击登录按钮后,&g应该打印出用户名和密码的值,但是显示为空:&g : &{ }
应该怎么做?
英文:
1)Rendered a Login Page through template method.
Ex.: this is index.html
{{ define "title" }}Guestbook{{ end }}
{{ define "content" }}
<form action="/login" method="post">
<div><label>UserName : </label><input name="username" type="text" /></div>
<div><label>Password : </label><input name="password" type="password" /></div>
<div><input type="submit" value="login"></div>
</form>
{{ end }}
-
hello.go file:
package main
import (
"fmt"
"html/template"
"net/http"
)var index = template.Must(template.ParseFiles(
"templates/base.html",
"templates/index.html",
))
type UserLogin struct{
UserName string
PassWord string
}func handler(w http.ResponseWriter, r *http.Request) {
index.Execute(w, nil)
}/*func login(w http.ResponseWriter, r http.Request) {
remPartOfURL := r.URL.Path[len("/login/"):]
if r.Method == "POST" {
fmt.Fprintf(w, "after",r.FormValue("username"))
}
fmt.Fprintf(w, "Hello %s!", remPartOfURL)
}/
func login(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "login : ", "\n")
remPartOfURL := r.URL.Path[len("/login/"):]
if r.Method == "POST" {
g := UserLogin{
UserName: r.FormValue("username"),
PassWord: r.FormValue("password"),
}
fmt.Fprint(w, "&g : ", &g,"\n")
}
fmt.Fprintf(w, "Hello %s!", remPartOfURL)
}func init() {
http.HandleFunc("/", handler)
http.HandleFunc("/login/", login)
}
Question: After clicked on Login button: &g should print username and password values but it is showing empty: &g : &{ }
What can be done?
答案1
得分: 0
你可以尝试将fmt.Fprintf
语句替换为http.Error
语句。
例如,将以下语句替换为:
fmt.Fprintf(w, "after", r.FormValue("username"))
替换为:
http.Error(w, fmt.Sprintf("after %s", r.FormValue("username")), http.StatusOK)
你正在直接向http.ResponseWriter
写入内容,这可能会引起问题。
英文:
One thing you might try is replacing the fmt.Fprintf
statements with http.Error
statements.
For example replace:
fmt.Fprintf(w, "after",r.FormValue("username"))
with:
http.Error(w, fmt.Sprintf("after %s", r.FormValue("username")), http.StatusOK)
You're writing directly to the http.ResponseWriter
which can cause problems.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论