dqsong2010 2016-03-26 15:24
浏览 478
已采纳

Gin-golang:使用样式表加载html文件

my english is very basic, I'm sorry :)

Well, my problem is that when Gin is loading HTML, my HTML file contains import (/stylesheet/index.css), so that when run my app with Gin, this alert that not load stylesheet file

workspace/
          main/
               main.go
          templates/
                   index.tmpl.html
                   css/
                       index.css

main.go

r.LoadHTMLFiles("../templates/index.tmpl.html")

r.GET("/index", func(c *gin.Context) {
    c.HTML(200, "index.tmpl.html", gin.H{
        "title": "Main website", //IGNORE THIS
    })
})

index.tmpl.html

<!DOCTYPE html>
<head>
  <title>Hola titulo</title>
  <style type="text/css" media="screen">
    <!-- 
        @import url("/css/index.css");
    -->
  </style>
</head>

<body>

  <header>
    <h1>City Gallery</h1>
  </header>

  <nav>
    London<br>
    Paris<br>
    Tokyo
  </nav>

  <section>
    <h1>London</h1>
    <p>London is the capital city of England. It is the most populous city in the United Kingdom,
with a metropolitan area of over 13 million inhabitants.</p>
    <p>Standing on the River Thames, London has been a major settlement for two millennia,
its history going back to its founding by the Romans, who named it Londinium.</p>
  </section>

  <footer>
    Copyright © W3Schools.com
  </footer>

</body>

</html>

index.css

header {
   background-color:black;
   color:white;
   text-align:center;
   padding:5px; 
}
nav {
   line-height:30px;
   background-color:#eeeeee;
   height:300px;
   width:100px;
   float:left;
   padding:5px; 
}
section {
   width:350px;
   float:left;
   padding:10px; 
}
footer {
   background-color:black;
   color:white;
   clear:both;
   text-align:center;
   padding:5px; 
}

So, I am runnning my app and this give me error 404 in Gin mode debug.

[GIN] 2016/03/26 - 12:10:50 | 200 |     688.938µs | 127.0.0.1 |   GET     /index
[GIN] 2016/03/26 - 12:10:50 | 404 |       2.865µs | 127.0.0.1 |   GET     /css/index.css

On the other hand when I load html file only, this shows the stylesheet without problems. :(

I do not know how to load the stylesheet with Gin.

Help, please.

PS: I need use Gin

  • 写回答

2条回答 默认 最新

  • doujiong9915 2016-03-26 15:38
    关注

    You need a handler for static files in main.go. Use Static.

    r.Static("/css", "../templates/css")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序