讲师中心 微信公众号
AI工具推荐 视频效率加速

技术博客建站教程——使用Beego开发

胖雪姑娘_5758

胖雪姑娘_5758

发布时间:2023-06-22 13:16:42

|

1811人浏览过

|

来源于php中文网

原创

一、前言
如今,技术博客已成为了程序员交流互动、展示技术、拓宽思路的重要平台之一。对于有一定编程基础的程序员来说,自己开发博客,实现个性化定制和自由扩展,已逐渐成为一种趋势。

本文将引导读者使用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() {

Beego框架 2.3.5
Beego框架 2.3.5

Beego框架 2.3.5 版本源码包下载,适合关注表单空值、函数注释名和 nil 返回值修复的 Go Web 开发者。

下载
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" .}}
<div>

<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>

</div>
{{template "footer.html" .}}

//article/add.html
{{template "header.html" .}}
<div>

<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>

</div>
{{template "footer.html" .}}

//article/update.html
{{template "header.html" .}}
<div>

<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>

</div>
{{template "footer.html" .}}

//article/detail.html
{{template "header.html" .}}
<div>

<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>

</div>
{{template "footer.html" .}}

五、运行项目
在终端使用bee run命令启动项目,然后访问http://localhost:8080/article/list即可访问博客。

六、总结
本文简单介绍了Beego框架的使用,在此基础上实现了一个简单的博客应用程序。通过本文的学习,读者可初步了解Beego框架的基本使用方法,更多详细内容请参考官方文档。

热门AI工具

更多
切问学术

切问学术是一款AI论文写作工具,复旦大学NLP团队推出的AI学术智能体。

讯飞智作

讯飞智作是一款AI视频创作工具,AI文本配音工具,数字人课程、营销视频制作。

DeepSeek

DeepSeek是一款面向对话、写作、编程和推理场景的AI大模型工具。

VibeKnow
VibeKnow Hot

一款AI视频创作工具,主要用于全球首个AI知识视频创作平台,文档、文章、网页,一键生成视频,适合需要提升相关任务效率的用户。

豆包大模型

豆包大模型是一款由字节跳动推出的企业级大语言模型服务平台。

UP简历
UP简历 Hot

一款AI办公效率工具,主要用于基于AI技术的免费在线简历制作工具,适合需要提升相关任务效率的用户。

Atoms
Atoms Hot

Atoms是一款AI智能体工具,第一支自动构建真实业务的 AI 团队。

AionClaw
AionClaw Hot

AionClaw是一款面向办公、创作和编程任务的AI桌面智能体。

WorkBuddy

一款AI办公效率工具,主要用于腾讯云推出的AI原生桌面智能体工作台,适合需要提升相关任务效率的用户。

相关专题

更多
LLVM自定义Pass怎么写
LLVM自定义Pass怎么写

本专题聚焦LLVM自定义Pass开发,整理Pass类结构、run()方法、PreservedAnalyses、CMake构建、插件注册、-load-pass-plugin加载和测试用例编写流程。

80

2026.09.30

LLVM RISC-V参数配置教程
LLVM RISC-V参数配置教程

本专题介绍LLVM对RISC-V基础ISA和扩展的支持方式,涵盖RV32、RV64、标准扩展、实验性扩展、厂商扩展、-menable-experimental-extensions和版本差异。

80

2026.09.30

LLVM IR中间表示入门指南
LLVM IR中间表示入门指南

本专题整理LLVM IR的核心概念,包括中间表示作用、模块结构、函数、基本块、SSA形式、类型系统和常见语法,帮助新手理解LLVM编译流程中的关键层。

80

2026.09.30

PDF转图片方法
PDF转图片方法

需要把 PDF 页面用于上传、预览、分享或图片归档时,PDF 转图片方法专题整理 JPG/PNG 格式选择、逐页导出、清晰度设置、批量下载和结果检查等流程,帮助用户稳定完成 PDF 图片化处理。

40

2026.09.30

PixTV AI视频生成与无限画布创作
PixTV AI视频生成与无限画布创作

PixTV专题整理AI视频与视觉内容创作相关功能使用教程,涵盖AI生图、视频生成、无限画布、多模型创作、素材管理、声音音乐及视频剪辑等功能,帮助用户快速掌握PixTV从创意到成片的完整制作方法。

60

2026.09.29

Buffalo框架数据库开发全教程
Buffalo框架数据库开发全教程

本专题围绕Buffalo框架数据库开发,讲解database.yml多环境配置、soda与fizz迁移生成回滚、模型结构体标签、增删改查与条件查询、一对多与多对多关联、数据校验、回调钩子、事务处理及原生SQL执行能力。

280

2026.09.23

Buffalo框架路由与请求处理实操指南
Buffalo框架路由与请求处理实操指南

本专题讲解Buffalo框架路由与请求处理机制,涵盖路由注册与分组、资源路由、Handler编写规范、Context上下文方法、参数绑定、中间件编写挂载、Session与Cookie读写、Flash消息及错误页面定制方法。

160

2026.09.23

Buffalo框架零基础入门教程
Buffalo框架零基础入门教程

本专题整理Buffalo框架入门内容,涵盖Go环境准备、buffalo CLI安装、新项目生成、目录结构说明、dev热加载启动、数据库连接配置与常见报错排查,帮助新手按约定优于配置的思路跑通第一个Buffalo框架应用。

140

2026.09.23

Conan创建软件包配方指南
Conan创建软件包配方指南

本专题介绍通过conanfile.py创建软件包的方法,讲解包名、版本、依赖和构建设置等基础信息,以及source、build、package、package_info等常用方法的作用及编写思路。

80

2026.09.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Beego 快速开始文档
Beego 快速开始文档

共0课时 | 0人学习

Beego 官方文档首页
Beego 官方文档首页

共0课时 | 0人学习

beego零基础到项目实战
beego零基础到项目实战

共0课时 | 0人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn