Golang中HTML模板无法加载的问题如何解决

Golang中HTML模板无法加载的问题如何解决 路由代码。

r := gin.Default()
r.LoadHTMLGlob("views/*")
Teacher := r.Group("/teacher", SessionHandler)
	{
		Teacher.GET("/teacherpanel", controllers.TeacherPanelController)
		Teacher.GET("/createquizz", controllers.CreateQuizController)
		Teacher.POST("/createquizz", controllers.PostQuizController)
		Teacher.GET("/addstudent", controllers.AddStudentController)
		Teacher.POST("/addstudent", controllers.PostStudentController)
		Teacher.GET("/listofquiz", controllers.GetListOfQuizController)
		Teacher.GET("/quiz",controllers.GetQuizController)

	}

控制器代码 GetQuizController

func GetQuizController(c *gin.Context) {

	fmt.Println("get quiz controller called")
	quizname := c.Query("quizname")
	var questions []models.Questions
	dbconnection.DB.Debug().Model(&models.Quiz{}).Select("quizzes.quiz_name,questions.question,questions.option_a,questions.option_b,questions.option_c,questions.option_d,questions.answer,questions.difficulty").Joins("inner join questions on questions.quiz_id=quizzes.id").Where("quizzes.quiz_name=?", quizname).Scan(&questions)
	fmt.Println(questions)

	c.HTML(200,"showquiz.html",gin.H{
		"data":questions,
	})
	

}

在这个 GetQuizController 中,c.HTML 无法正常工作。请帮助我。 这里是 GitHub 仓库的链接:github 链接


更多关于Golang中HTML模板无法加载的问题如何解决的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于Golang中HTML模板无法加载的问题如何解决的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


根据你的代码和仓库结构,问题在于模板文件路径配置不正确。以下是解决方案:

问题分析:

  1. 你使用 r.LoadHTMLGlob("views/*") 加载模板,但仓库中模板文件位于 templates/ 目录下
  2. 控制器中调用 c.HTML(200,"showquiz.html",...),但实际文件名为 showquiz.html

解决方案:

1. 修正模板加载路径(main.go 中):

// 修改为正确的模板目录
r.LoadHTMLGlob("templates/*")

// 或者如果需要包含子目录
r.LoadHTMLGlob("templates/**/*")

2. 确保模板文件存在: 检查 templates/ 目录下确实存在 showquiz.html 文件

3. 完整的修正示例:

// main.go 中的路由配置
func main() {
    r := gin.Default()
    
    // 修正模板加载路径
    r.LoadHTMLGlob("templates/*")
    
    // 或者如果模板在子目录中
    // r.LoadHTMLGlob("templates/**/*")
    
    Teacher := r.Group("/teacher", SessionHandler)
    {
        Teacher.GET("/quiz", controllers.GetQuizController)
        // ... 其他路由
    }
    
    r.Run(":8080")
}

4. 调试建议: 在控制器中添加模板加载验证:

func GetQuizController(c *gin.Context) {
    // 检查模板是否存在
    if gin.Mode() == gin.DebugMode {
        fmt.Println("Looking for template: showquiz.html")
    }
    
    quizname := c.Query("quizname")
    var questions []models.Questions
    
    dbconnection.DB.Debug().Model(&models.Quiz{}).
        Select("quizzes.quiz_name,questions.question,questions.option_a,questions.option_b,questions.option_c,questions.option_d,questions.answer,questions.difficulty").
        Joins("inner join questions on questions.quiz_id=quizzes.id").
        Where("quizzes.quiz_name=?", quizname).
        Scan(&questions)
    
    // 渲染模板
    c.HTML(200, "showquiz.html", gin.H{
        "data": questions,
    })
}

5. 如果问题仍然存在,尝试绝对路径:

// 使用绝对路径确保正确加载
r.LoadHTMLGlob(filepath.Join(getCurrentDir(), "templates/*"))

func getCurrentDir() string {
    ex, err := os.Executable()
    if err != nil {
        panic(err)
    }
    return filepath.Dir(ex)
}

根据你的仓库结构,主要问题是 LoadHTMLGlob 的路径参数需要从 "views/*" 改为 "templates/*"

回到顶部