如何在Go编程中创建表单模板?

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

How to Create Form Template in Go Programming?

问题

如何在Go编程中创建登录表单(用户名和密码)模板?

英文:

How to Create Login Form (username and Password) Template in Go Programming?

答案1

得分: 1

使用template在appengine上只有在传递html字段时才可能,因为根据appengine的规则,您无法访问文件系统。

这是一个示例:

const loginTemplateHTML = `<html>
  <body>
    <form action="/login" method="post">
	  <div><input name="username" type="text" /></div>
	  <div><input name="password" type="password" /></div>
      <div><input type="submit" value="login"></div>
    </form>
  </body>
</html>
    `    
var loginTemplate = template.Must(template.New("Login").Parse(loginTemplateHTML))
    
func login (w http.ResponseWriter, r *http.Request) {
    if err := loginTemplate.Execute(w,nil); err != nil {
         http.Error(w, err.String(), http.StatusInternalServerError)
    }
}
英文:

using template on appengine is only posible if you pass the html from field, because of appengine rules you dont have access to the filesystem

here is an example

const loginTemplateHTML = `<html>
  <body>
    <form action="/login" method="post">
	  <div><input name="username" type="text" /></div>
	  <div><input name="password" type="password" /></div>
      <div><input type="submit" value="login"></div>
    </form>
  </body>
</html>
    `    
var loginTemplate = template.Must(template.New("Login").Parse(loginTemplateHTML))
    
func login (w http.ResponseWriter, r *http.Request) {
    if err := loginTemplate.Execute(w,nil); err != nil {
         http.Error(w, err.String(), http.StatusInternalServerError)
    }
}

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

发表评论

匿名网友

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

确定