技术博客建站教程——使用Beego开发
对于一个Golang开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《技术博客建站教程——使用Beego开发》,主要介绍了,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!
一、前言
如今,技术博客已成为了程序员交流互动、展示技术、拓宽思路的重要平台之一。对于有一定编程基础的程序员来说,自己开发博客,实现个性化定制和自由扩展,已逐渐成为一种趋势。
本文将引导读者使用Beego框架搭建自己的技术博客,旨在提供一种方便高效且易于扩展的解决方案。
二、Beego框架简介
Beego是基于Go语言开发的Web框架,它的设计灵感来自于Python的Django框架和Python的Tornado框架。Beego是一个轻量级、简单易学、高效灵活的Web框架,同时也支持RESTful API开发。
三、环境搭建
1、安装Go环境
首先需要安装Go环境,具体步骤可查阅官方文档进行安装。
2、安装Beego和Bee工具
Beego和Bee是两个不同的工具,Beego是核心框架,而Bee是一个基于Beego框架的命令行工具,可用于新建项目、创建Controller、Model、View等,大大提高了开发效率。
使用命令安装:go get github.com/astaxie/beego
go get github.com/beego/bee
3、创建项目及配置
创建一个名为myblog的项目:bee new myblog
然后进入myblog目录:cd myblog
现在在myblog目录下会有一个名为conf的文件夹,里面的app.conf即为配置文件,我们可以在这里进行相关配置,如数据库的连接地址、端口等。
四、实现博客功能
1、模型设计
首先,需在models目录下编写blog.go文件,用于创建数据库的表,如下所示:
package models
import (
"github.com/astaxie/beego/orm" "time"
)
//数据结构
//文章
type Article struct {
Id int64 `orm:"auto"` Title string `orm:"size(100)"` Content string `orm:"type(text)"` ImgUrl string `orm:"size(200)"` Category *Category `orm:"-"` Created time.Time `orm:"auto_now_add;type(datetime)"` Updated time.Time `orm:"auto_now_add;type(datetime)"`
}
//分类
type Category struct {
Id int64 Title string Articles []*Article `orm:"reverse(many)"`
}
2、Controller编写
在controllers目录下编写article.go文件,用于实现与文章相关的控制器方法,如下所示:
package controllers
import (
"myblog/models" "fmt" "strconv" "time"
)
type ArticleController struct {
BaseController
}
func (this *ArticleController) List() {
categoryIdStr := this.GetString("category_id")
categoryId, _ := strconv.ParseInt(categoryIdStr, 10, 64)
categories := models.GetAllCategory()
this.Data["Categories"] = categories
var articles []*models.Article
if categoryId == 0 {
articles = models.GetAllArticle()
} else {
articles = models.GetArticleByCategory(categoryId)
}
this.Data["Articles"] = articles
this.Data["CategoryId"] = categoryId
this.TplName = "article/list.html"}
func (this *ArticleController) Add() {
if this.Ctx.Request.Method == "GET" {
categories := models.GetAllCategory()
this.Data["Categories"] = categories
this.TplName = "article/add.html"
return
}
title := this.GetString("title")
content := this.GetString("content")
categoryId, _ := this.GetInt64("category_id")
imgUrl := this.GetString("img_url")
article := models.Article{Title: title, Content:content, ImgUrl:imgUrl, Category:&models.Category{Id:categoryId}}
models.AddArticle(&article)
fmt.Println("添加成功")
this.Redirect("/article/list", 302)}
func (this *ArticleController) Update() {
id, _ := this.GetInt64(":id")
if this.Ctx.Request.Method == "GET" {
article := models.GetArticleById(id)
this.Data["Article"] = article
categories := models.GetAllCategory()
this.Data["Categories"] = categories
this.TplName = "article/update.html"
return
}
title := this.GetString("title")
content := this.GetString("content")
categoryId, _ := this.GetInt64("category_id")
imgUrl := this.GetString("img_url")
article := models.Article{Id: id, Title: title, Content:content, ImgUrl:imgUrl, Category:&models.Category{Id:categoryId}}
models.UpdateArticle(&article)
this.Redirect("/article/list", 302)}
func (this *ArticleController) Delete() {
id, _ := this.GetInt64(":id")
models.DeleteArticleById(id)
this.Redirect("/article/list", 302)}
func (this *ArticleController) Detail() {
id, _ := this.GetInt64(":id")
article := models.GetArticleById(id)
this.Data["Article"] = article
this.TplName = "article/detail.html"}
3、View视图文件
在views目录下编写article目录,存放article相关的视图文件,如下所示:
//article/list.html
{{template "header.html" .}}
<h3>文章管理</h3>
<div class="list-nav">
<a href="{{.ctx.Request.URL.Path}}">全部</a>
{{range .Categories}}
<a href="{{.ctx.Request.URL.Path}}?category_id={{.Id}}">{{.Title}}</a>
{{end}}
</div>
<table>
<thead>
<tr>
<th>Id</th>
<th>标题</th>
<th>分类</th>
<th>发布时间</th>
<th>更新时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{{range .Articles}}
<tr>
<td>{{.Id}}</td>
<td>{{.Title}}</td>
<td>{{.Category.Title}}</td>
<td>{{.Created.Format "2006-01-02 15:04:05"}}</td>
<td>{{.Updated.Format "2006-01-02 15:04:05"}}</td>
<td>
<a href="/article/detail?id={{.Id}}">查看</a>
<a href="/article/update?id={{.Id}}">修改</a>
<a href="/article/delete?id={{.Id}}" onclick="return confirm('确定删除文章【{{.Title}}】吗?')">删除</a>
</td>
</tr>
{{end}}
</tbody>
</table>{{template "footer.html" .}}
//article/add.html
{{template "header.html" .}}
<h3>添加文章</h3>
<form action="/article/add" method="post">
<p>标题: <input type="text" name="title"></p>
<p>
分类:
<select name="category_id">
{{range .Categories}}
<option value="{{.Id}}">{{.Title}}</option>
{{end}}
</select>
</p>
<p>图片Url: <input type="text" name="img_url"></p>
<p>内容: <textarea name="content"></textarea></p>
<p><input type="submit" value="添加"></p>
</form>{{template "footer.html" .}}
//article/update.html
{{template "header.html" .}}
<h3>修改文章</h3>
<form action="/article/update?id={{.Article.Id}}" method="post">
<p>标题: <input type="text" name="title" value="{{.Article.Title}}"></p>
<p>
分类:
<select name="category_id">
{{range $index, $option := .Categories}}
<option value="{{$option.Id}}" {{if eq $option.Id $.Article.Category.Id}}selected{{end}}>{{$option.Title}}</option>
{{end}}
</select>
</p>
<p>图片Url: <input type="text" name="img_url" value="{{.Article.ImgUrl}}"></p>
<p>内容: <textarea name="content" rows="30">{{.Article.Content}}</textarea></p>
<p><input type="submit" value="修改"></p>
</form>{{template "footer.html" .}}
//article/detail.html
{{template "header.html" .}}
<h3>{{.Article.Title}}</h3>
<p>分类:{{.Article.Category.Title}}</p>
<p>发布时间:{{.Article.Created.Format "2006-01-02 15:04:05"}}</p>
<p>更新时间:{{.Article.Updated.Format "2006-01-02 15:04:05"}}</p>
<p>内容:</p>
<div class="detail-content">{{.Article.Content}}</div>{{template "footer.html" .}}
五、运行项目
在终端使用bee run命令启动项目,然后访问http://localhost:8080/article/list即可访问博客。
六、总结
本文简单介绍了Beego框架的使用,在此基础上实现了一个简单的博客应用程序。通过本文的学习,读者可初步了解Beego框架的基本使用方法,更多详细内容请参考官方文档。
今天关于《技术博客建站教程——使用Beego开发》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于Beego,建站教程,技术博客的内容请关注golang学习网公众号!
机器人造房如造车!广东企业强大阵容亮相住博会
- 上一篇
- 机器人造房如造车!广东企业强大阵容亮相住博会
- 下一篇
- Golang中使用缓存处理语音生成算法的技巧。
-
- Golang · Go教程 | 1分钟前 |
- Golang结构体嵌套使用技巧详解
- 304浏览 收藏
-
- Golang · Go教程 | 7分钟前 |
- Golang搭建CI/CD流水线教程
- 105浏览 收藏
-
- Golang · Go教程 | 9分钟前 |
- Golang博客生成器:模板与构建流程解析
- 118浏览 收藏
-
- Golang · Go教程 | 18分钟前 |
- Golangcrypto加密解密方法全解析
- 180浏览 收藏
-
- Golang · Go教程 | 19分钟前 | 依赖管理 GoModules go.mod 多版本 replace/exclude
- Golang多版本依赖管理方法
- 467浏览 收藏
-
- Golang · Go教程 | 24分钟前 |
- GolangTLS加密HTTP连接详解
- 103浏览 收藏
-
- Golang · Go教程 | 31分钟前 |
- GolangRPC客户端错误处理技巧
- 492浏览 收藏
-
- Golang · Go教程 | 36分钟前 | 性能分析 Go语言 sub-benchmark testing.B.Run 细粒度测试
- Golangsub-benchmark精细测试技巧
- 399浏览 收藏
-
- Golang · Go教程 | 51分钟前 |
- Golang路由匹配规则与优先级详解
- 488浏览 收藏
-
- Golang · Go教程 | 1小时前 | GoModules replace指令 go.work GoWorkspace 多模块开发
- GoWorkspace创建与配置全攻略
- 304浏览 收藏
-
- Golang · Go教程 | 1小时前 |
- gRPC拦截器使用详解与实战教程
- 350浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- ChatExcel酷表
- ChatExcel酷表是由北京大学团队打造的Excel聊天机器人,用自然语言操控表格,简化数据处理,告别繁琐操作,提升工作效率!适用于学生、上班族及政府人员。
- 3203次使用
-
- Any绘本
- 探索Any绘本(anypicturebook.com/zh),一款开源免费的AI绘本创作工具,基于Google Gemini与Flux AI模型,让您轻松创作个性化绘本。适用于家庭、教育、创作等多种场景,零门槛,高自由度,技术透明,本地可控。
- 3416次使用
-
- 可赞AI
- 可赞AI,AI驱动的办公可视化智能工具,助您轻松实现文本与可视化元素高效转化。无论是智能文档生成、多格式文本解析,还是一键生成专业图表、脑图、知识卡片,可赞AI都能让信息处理更清晰高效。覆盖数据汇报、会议纪要、内容营销等全场景,大幅提升办公效率,降低专业门槛,是您提升工作效率的得力助手。
- 3446次使用
-
- 星月写作
- 星月写作是国内首款聚焦中文网络小说创作的AI辅助工具,解决网文作者从构思到变现的全流程痛点。AI扫榜、专属模板、全链路适配,助力新人快速上手,资深作者效率倍增。
- 4554次使用
-
- MagicLight
- MagicLight.ai是全球首款叙事驱动型AI动画视频创作平台,专注于解决从故事想法到完整动画的全流程痛点。它通过自研AI模型,保障角色、风格、场景高度一致性,让零动画经验者也能高效产出专业级叙事内容。广泛适用于独立创作者、动画工作室、教育机构及企业营销,助您轻松实现创意落地与商业化。
- 3824次使用
-
- Golangmap实践及实现原理解析
- 2022-12-28 505浏览
-
- go和golang的区别解析:帮你选择合适的编程语言
- 2023-12-29 503浏览
-
- 试了下Golang实现try catch的方法
- 2022-12-27 502浏览
-
- 如何在go语言中实现高并发的服务器架构
- 2023-08-27 502浏览
-
- 提升工作效率的Go语言项目开发经验分享
- 2023-11-03 502浏览

