From bfa17eaf1371684b12ebbd1f9f883c1b98ee6dc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=B6=85?= <17805310388@139.com> Date: Mon, 5 Aug 2024 17:19:23 +0800 Subject: [PATCH] add post page --- main.go | 20 +-- templates/index.tmpl | 214 +++++++++++++++------------- templates/page2 copy.tmpl | 12 -- templates/page2.tmpl | 12 -- templates/page3.tmpl | 12 -- templates/{page1.tmpl => post.tmpl} | 6 +- 6 files changed, 125 insertions(+), 151 deletions(-) delete mode 100644 templates/page2 copy.tmpl delete mode 100644 templates/page2.tmpl delete mode 100644 templates/page3.tmpl rename templates/{page1.tmpl => post.tmpl} (77%) diff --git a/main.go b/main.go index d0cee99..f33ce57 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "go_blog/controllers" "go_blog/models" "net/http" + "strconv" "github.com/gin-gonic/gin" ) @@ -60,15 +61,17 @@ func registerRoutes(r *gin.Engine) { r.GET("/createcontent", func(c *gin.Context) { c.HTML(http.StatusOK, "content.tmpl", nil) }) - user := getUserInfo() - r.GET("/page/:id", func(c *gin.Context) { - id := c.Param("id") - c.HTML(http.StatusOK, "page"+id+".tmpl", map[string]interface{}{ - "title": "这个是titile,传入templates中的", - "user": getUserInfo, - }) - }) + r.GET("/post/:id", func(c *gin.Context) { + id, err := strconv.ParseInt(c.Param("id"), 10, 32) + if err != nil { + return + } + var content = models.Content{Cid: int32(id)} + models.DB.First(&content) + c.HTML(http.StatusOK, "post.tmpl", content) + }) + user := getUserInfo() r.GET("/login", func(c *gin.Context) { c.HTML(200, "login.tmpl", map[string]interface{}{ "title": "这个是titile,传入templates中的", @@ -77,7 +80,6 @@ func registerRoutes(r *gin.Engine) { }) r.GET("/ws", controllers.WebSocketHandler) - r.POST("/content", controllers.CreateContentHandler) r.POST("/login", controllers.UsersLoginHandler) } diff --git a/templates/index.tmpl b/templates/index.tmpl index 70d3806..d6f09c6 100644 --- a/templates/index.tmpl +++ b/templates/index.tmpl @@ -1,118 +1,126 @@ - My Blog - - - + My Blog + + + + -
-
-

Welcome to my blog!

- +
+
+

Welcome to my blog!

+
+ +
+

Go语言入门 + Gin框架入门 + Web开发基础

+
-
- -

Go语言入门 - Gin框架入门 - Web开发基础

-
-
- - - - - - -
- -
- - {{ range .Items }} -
-

Title: {{ .Title }}

Slug: {{ .Slug }}

-

Text: {{ .Text }}

-
Created: {{ .Created }},
-
- {{ end }} -
-