Home > Backend Development > Golang > Technical blog website building tutorial - developed using Beego

Technical blog website building tutorial - developed using Beego

WBOY
Release: 2023-06-22 13:16:42
Original
1247 people have browsed it

1. Foreword
Today, technology blogs have become one of the important platforms for programmers to communicate, interact, display technology, and broaden their ideas. For programmers with a certain programming foundation, it has gradually become a trend to develop their own blogs to achieve personalized customization and free expansion.

This article will guide readers to use the Beego framework to build their own technology blog, aiming to provide a convenient, efficient and easy-to-expand solution.

2. Introduction to Beego framework
Beego is a Web framework developed based on Go language. Its design is inspired by Python's Django framework and Python's Tornado framework. Beego is a lightweight, easy-to-learn, efficient and flexible web framework that also supports RESTful API development.

3. Environment setup
1. Install the Go environment
First you need to install the Go environment. For specific steps, please refer to the official documentation for installation.
2. Install Beego and Bee tools
Beego and Bee are two different tools. Beego is the core framework, and Bee is a command line tool based on the Beego framework, which can be used to create new projects, create Controllers, Models, View, etc., greatly improve development efficiency.

Use command to install: go get github.com/astaxie/beego
go get github.com/beego/bee

3. Create project and configuration
Create a project named Myblog project: bee new myblog
Then enter the myblog directory: cd myblog
Now there will be a folder named conf in the myblog directory. The app.conf inside is the configuration file. We can do it here Related configurations, such as database connection address, port, etc.

4. Implement blog function
1. Model design
First, you need to write the blog.go file in the models directory to create the database table, as shown below:

package models

import (

"github.com/astaxie/beego/orm"
"time"
Copy after login

)

//Data structure
//Article
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)"`
Copy after login

}

//Category
type Category struct {

Id       int64
Title    string
Articles []*Article `orm:"reverse(many)"`
Copy after login

}

2. Controller writing
Write the article.go file in the controllers directory for implementation The controller methods related to articles are as follows:

package controllers

import (

"myblog/models"
"fmt"
"strconv"
"time"
Copy after login

)

type ArticleController struct {

BaseController
Copy after login

}

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"
Copy after login

}

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)
Copy after login

}

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)
Copy after login

}

func (this *ArticleController) Delete() {

id, _ := this.GetInt64(":id")
models.DeleteArticleById(id)

this.Redirect("/article/list", 302)
Copy after login

}

func (this *ArticleController) Detail() {

id, _ := this.GetInt64(":id")

article := models.GetArticleById(id)
this.Data["Article"] = article

this.TplName = "article/detail.html"
Copy after login

}

3. View file
Write the article directory in the views directory to store article-related information View file, as shown below:

//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>
Copy after login

< /div>
{{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>
Copy after login


{{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>
Copy after login


{{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>
Copy after login


{{template "footer.html" .}}

5. Run the project
Use the bee run command in the terminal to start the project, and then visit http://localhost:8080/article/list to access the blog.

6. Summary
This article briefly introduces the use of Beego framework, and implements a simple blog application on this basis. By studying this article, readers can have a preliminary understanding of the basic usage of the Beego framework. For more details, please refer to the official documentation.

The above is the detailed content of Technical blog website building tutorial - developed using Beego. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template