如何将数据POST到数据存储中?

huangapple go评论96阅读模式
英文:

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 }}
  1. 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 &quot;title&quot; }}Guestbook{{ end }}
    
    {{ define &quot;content&quot; }}
    	&lt;form action=&quot;/login&quot; method=&quot;post&quot;&gt;
          &lt;div&gt;&lt;label&gt;UserName : &lt;/label&gt;&lt;input name=&quot;username&quot; type=&quot;text&quot; /&gt;&lt;/div&gt;
          &lt;div&gt;&lt;label&gt;Password : &lt;/label&gt;&lt;input name=&quot;password&quot; type=&quot;password&quot; /&gt;&lt;/div&gt;
          &lt;div&gt;&lt;input type=&quot;submit&quot; value=&quot;login&quot;&gt;&lt;/div&gt;
        &lt;/form&gt;
    	
    {{ end }}
  1. 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, &quot;after&quot;,r.FormValue(&quot;username&quot;))

with:

http.Error(w, fmt.Sprintf(&quot;after %s&quot;, r.FormValue(&quot;username&quot;)), http.StatusOK)

You're writing directly to the http.ResponseWriter which can cause problems.

huangapple
  • 本文由 发表于 2013年6月18日 16:23:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/17163932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定