恐慌:打开模板/base.html时出错:系统找不到指定的路径

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

panic: open templates/base.html: The system cannot find the path specified

问题

我正在尝试使用shadynasty.biz/blog/2012/07/30/quick-and-clean-in-go链接在Go编程中创建模板。我的项目结构是使用Go-SDK Google App Engine创建的。google_appengine/myapp/hello/hello.go文件已经存在,那么在哪里创建模板文件夹?我在"hello"文件夹中创建了"template"文件夹,但是出现错误"panic: open templates/base.html: The system cannot find the path specified",服务器停止运行。应该怎么办?

英文:

With shadynasty.biz/blog/2012/07/30/quick-and-clean-in-go link I am Trying to Create template in go programming. My project Structure is created with the Go-SDK google app engine. google_appengine/myapp/hello/hello.go file is present, then where to create template folder? I created the "template" folder in "hello" folder but getting error "panic: open templates/base.html: The system cannot find the path specified" and server stops running. What can be done?

答案1

得分: 3

实际上,您确实可以访问您放置在应用程序文件夹中的几乎任何文件。

您可以使用ioutil来读取文件:
http://golang.org/pkg/io/ioutil/#ReadFile

需要注意的是,您只能读取app.yaml中未指定为静态处理程序的文件。

因此,请将模板文件放在一个文件夹中,将静态文件和直接提供的文件放在另一个文件夹中。

英文:

Actually you do have access to almost any file you put to your application folder.

You could use the ioutil to read the files:
http://golang.org/pkg/io/ioutil/#ReadFile

And a point to note, you can only read files not specified by the static handler in app.yaml.

So, keep your template files in one folder and your static and directly served files in another folder.

答案2

得分: 0

使用模板在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.Error(), 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.Error(), http.StatusInternalServerError)
    }
}

答案3

得分: 0

问题是应用引擎不会像go run hello.go那样调用你的应用程序。它是从根目录go run google_appengine/myapp/hello/hello.go调用的。你有两种解决方法来修复这个问题。

  1. 重新定义要从根目录调用的路径。

我推荐并验证了这个方法,只需将路径从项目的根目录添加到模板文件夹。例如,如果项目的根目录是myRepo/,webapp的路径是/myRepo/webapp/hello.go,那么模板现在位于webapp/templates/index.html而不是templates/index.html。为了像应用引擎一样调用它,你将从根目录调用它。go run webapp/main.go。

  1. 在app.yaml中定义自定义入口点。

我还没有验证这个解决方案,但我认为它会起作用。在app.yaml中,有一个entrypoint命令用于启动代码。

entrypoint: cd myRepo/webapp/ && go run main.go

这是app.yaml的文档。确保为你的静态目录添加URL。

https://cloud.google.com/appengine/docs/standard/go/config/appref

英文:

The problem is app engine does not call your application like go run hello.go. It calls from the root directory go run google_appengine/myapp/hello/hello.go. You have two solutions to fixed this problem.

  1. Redefine the paths to be called from the root directory.

What I recommend and verified works is just setting adding the path from the root of the project to the template folder. For example, if the root of the project is myRepo/ and the path to the webapp is /myRepo/webapp/hello.go, then the template is now at webapp/templates/index.html instead of templates/index.html. In order to call it similar to app engine, you will call it from the root directory. go run webapp/main.go.

  1. Define a custom entrypoint in the app.yaml.

I have not verified this solutions yet, but I think it will work. In the app.yaml, there is a entrypoint command is the command use to startup the code.

entrypoint: cd myRepo/webapp/ && go run main.go

Here is the documentation for app.yaml. Make sure add urls for your static dir.

https://cloud.google.com/appengine/docs/standard/go/config/appref

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

发表评论

匿名网友

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

确定