dtczp02204 2013-06-18 10:14
浏览 54

前往:如何在数据存储中“放置” r.FormValue,以及如何从数据存储中“获取”?

Example:
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 }}

2) hello.go file:

package main 

import (
  "fmt"
  "html/template"
  "net/http"
)   

var index = template.Must(template.ParseFiles(
  "templates/base.html",
  "templates/index.html",
))
//UserLogin struct is created
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" {          
    http.Error(w, fmt.Sprintf("First Name: %s", r.FormValue("username")), http.StatusOK)           
    http.Error(w, fmt.Sprintf("Password: %s", r.FormValue("password")), http.StatusOK)          
    }           
}

func init() {
    http.HandleFunc("/", handler)
    http.HandleFunc("/login/", login)
}

In this Example in login(): Able to print the r.FormValue("username") and r.FormValue("password") but how to "put" in datastore and how to "get" from datastore.

  • 写回答

1条回答 默认 最新

  • donglianjiang9321 2013-06-18 10:45
    关注

    Reference : https://github.com/stephenlewis/eveningwithgo/blob/master/datastore/datastore/datastore.go

    Answer :

    package main

    import (
      "fmt"
      "html/template"
      "net/http"
      "appengine"
      "appengine/datastore"
    )
    
    var index = template.Must(template.ParseFiles(
      "templates/base.html",
      "templates/index.html",
    ))
    
    type cUserLogin struct{
        UserName string
        PassWord string
    }
    
    func handler(w http.ResponseWriter, r *http.Request) {  
        index.Execute(w, nil)
        fmt.Fprint(w, "handler : ", "
    ")
        c := appengine.NewContext(r)
        q := datastore.NewQuery("cUserLogin")
            w.Header().Add("Content-Type", "text/plain")
            for t := q.Run(c); ; {
                var getuser cUserLogin
                key, err := t.Next(&getuser)
                if err == datastore.Done {
                    break
                }
                fmt.Fprintf(w, "%v: %s %s
    ", key, getuser.UserName, getuser.PassWord)
            }
    
    }
    
    func login(w http.ResponseWriter, r *http.Request) {  
            remPartOfURL := r.URL.Path[len("/login/"):] 
            c := appengine.NewContext(r)
            if r.Method == "POST" {            
                http.Error(w, fmt.Sprintf("First Name: %s", r.FormValue("username")), http.StatusOK)           
                http.Error(w, fmt.Sprintf("Password: %s", r.FormValue("password")), http.StatusOK)
                g := cUserLogin{
                    UserName: r.FormValue("username"),
                    PassWord: r.FormValue("password"),
                }
                key, err := datastore.Put(c, datastore.NewIncompleteKey(c, "cUserLogin", nil), &g)
                if err != nil {
                    http.Error(w, err.Error(), http.StatusInternalServerError)
                    return
                }
                fmt.Fprintf(w, "Written with key %v
    ", key)            
            }       
    
            fmt.Fprintf(w, "Hello %s!", remPartOfURL)
        }
    
    
    func init() {
        http.HandleFunc("/", handler)
        http.HandleFunc("/login/", login)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。