博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
beeblog创建博客
阅读量:6079 次
发布时间:2019-06-20

本文共 6124 字,大约阅读时间需要 20 分钟。

1、定义modles.go

这里需要提前get一些包,否则会提示找不到哦,

models/models.go:5:2:

go get github.com/mattn/go-sqlite3

go get github.com/unknwon/com

package modelsimport (    "os"    "path"    "time"    "github.com/astaxie/beego/orm"    _ "github.com/mattn/go-sqlite3"    "github.com/unknwon/com")const (    _DB_NAME         = "data/beeblog.db"    _SQLLITE3_DRIVER = "sqlite3")type Categeory struct {    Id              int64    Title           string    Created         time.Time `orm:"index"`    views           int64     `orm:"index"`    TopicTime       time.Time `orm:"index"`    TopicCount      int64    TopicLastUserId int64}type Topic struct {    Id              int64    Uid             int64    Title           string    Content         string `orm:"size(5000)"`    Attachment      string    Created         time.Time `orm:"index"`    Updated         time.Time `orm:"index"`    views           int64     `orm:"index"`    Author          string    ReplyTime       time.Time `orm:"index"`    ReplyCount      int64    ReplyLastUserId int64}func RegisterDB() {    if !com.IsExist(_DB_NAME) {        os.MkdirAll(path.Dir(_DB_NAME), os.ModePerm) //创建/a/b/c/d目录        os.Create(_DB_NAME)                          //数据库文件不存在的时候会自动创建    }    orm.RegisterModel(new(Categeory), new(Topic))                   //注册模型    orm.RegisterDriver(_SQLLITE3_DRIVER, orm.DRSqlite)              //注册驱动    orm.RegisterDataBase("default", _SQLLITE3_DRIVER, _DB_NAME, 10) //注册默认数据库}

2 定义home.html 前端的页面

这里需要下载bootsstrap文件到static文件下

      首页 - 我的beego博客>    

3 定义main.go

package mainimport (    "beeblog/models"    _ "beeblog/routers"    "github.com/astaxie/beego"    "github.com/astaxie/beego/orm")func init() {    models.RegisterDB()}func main() {    orm.Debug = true                      //debug在开发模式下设置为true    orm.RunSyncdb("default", false, true) //必须有一个数据库名字叫default,false表示不是每次都删除重建表,首次建表即可,true表示打印建表信息    beego.Run()}

执行:bee run beeblog

beeblog2018/07/31 16:18:19 SUCCESS  ▶ 0006 Built Successfully!2018/07/31 16:18:19 INFO     ▶ 0007 Restarting 'beeblog'...2018/07/31 16:18:19 SUCCESS  ▶ 0008 './beeblog' is running...create table `categeory`    -- --------------------------------------------------    --  Table Structure for `beeblog/models.Categeory`    -- --------------------------------------------------    CREATE TABLE IF NOT EXISTS `categeory` (        `id` integer NOT NULL PRIMARY KEY AUTOINCREMENT,        `title` varchar(255) NOT NULL DEFAULT '' ,        `created` datetime NOT NULL,        `topic_time` datetime NOT NULL,        `topic_count` integer NOT NULL DEFAULT 0 ,        `topic_last_user_id` integer NOT NULL DEFAULT 0    );    CREATE INDEX `categeory_created` ON `categeory` (`created`);    CREATE INDEX `categeory_topic_time` ON `categeory` (`topic_time`);create table `topic`    -- --------------------------------------------------    --  Table Structure for `beeblog/models.Topic`    -- --------------------------------------------------    CREATE TABLE IF NOT EXISTS `topic` (        `id` integer NOT NULL PRIMARY KEY AUTOINCREMENT,        `uid` integer NOT NULL DEFAULT 0 ,        `title` varchar(255) NOT NULL DEFAULT '' ,        `content` varchar(5000) NOT NULL DEFAULT '' ,        `attachment` varchar(255) NOT NULL DEFAULT '' ,        `created` datetime NOT NULL,        `updated` datetime NOT NULL,        `author` varchar(255) NOT NULL DEFAULT '' ,        `reply_time` datetime NOT NULL,        `reply_count` integer NOT NULL DEFAULT 0 ,        `reply_last_user_id` integer NOT NULL DEFAULT 0    );    CREATE INDEX `topic_created` ON `topic` (`created`);    CREATE INDEX `topic_updated` ON `topic` (`updated`);    CREATE INDEX `topic_reply_time` ON `topic` (`reply_time`);2018/07/31 16:18:19.789 [I] [asm_amd64.s:2337] http server Running on http://:80802018/07/31 16:18:41.650 [D] [server.go:2619] ::1 - - [31/Jul/2018 04:18:41] "GET / HTTP/1.1 200 0" 0.005279  Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.362018/07/31 16:18:41.667 [D] [server.go:2619] ::1 - - [31/Jul/2018 04:18:41] "GET /static/css/bootstrap.min.css HTTP/1.1 200 0" 0.002514 http://localhost:8080/ Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.362018/07/31 16:18:43.342 [D] [server.go:2619] ::1 - - [31/Jul/2018 04:18:43] "GET /category HTTP/1.1 404 0" 0.003407 http://localhost:8080/ Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36beeblog2018/07/31 16:23:05 SUCCESS  ▶ 0009 Built Successfully!2018/07/31 16:23:05 INFO     ▶ 0010 Restarting 'beeblog'...2018/07/31 16:23:05 SUCCESS  ▶ 0011 './beeblog' is running...table `categeory` already exists, skip //这里表已经存在,跳过创建table `topic` already exists, skip2018/07/31 16:23:05.792 [I] [asm_amd64.s:2337] http server Running on http://:80802018/07/31 16:24:34.034 [D] [server.go:2619] ::1 - - [31/Jul/2018 04:24:34] "GET / HTTP/1.1 200 0" 0.011260  Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36

浏览器访问:

beeblog创建博客

修改home.html

      首页 - 我的beego博客>    

浏览器访问:

beeblog创建博客

4 如果使用js或者jquery,在home.html加上两行:

其中,jquery使用的是七牛云存储提供的jquery地址,查询jquery地址:

      首页 - 我的beego博客>    

博客的分类和登陆

转载于:https://blog.51cto.com/daixuan/2152863

你可能感兴趣的文章
DEV实现日期时间效果
查看>>
java注解【转】
查看>>
centos 下安装g++
查看>>
嵌入式,代码调试----GDB扫盲
查看>>
下一步工作分配
查看>>
Response. AppendHeader使用大全及文件下载.net函数使用注意点(转载)
查看>>
Wait Functions
查看>>
jQuery最佳实践
查看>>
centos64i386下apache 403没有权限访问。
查看>>
jquery用法大全
查看>>
PC-BSD 9.2 发布,基于 FreeBSD 9.2
查看>>
css斜线
查看>>
Windows phone 8 学习笔记(3) 通信
查看>>
Revit API找到风管穿过的墙(当前文档和链接文档)
查看>>
Scroll Depth – 衡量页面滚动的 Google 分析插件
查看>>
Windows 8.1 应用再出发 - 视图状态的更新
查看>>
自己制作交叉编译工具链
查看>>
Qt Style Sheet实践(四):行文本编辑框QLineEdit及自动补全
查看>>
[物理学与PDEs]第3章习题1 只有一个非零分量的磁场
查看>>
深入浅出NodeJS——数据通信,NET模块运行机制
查看>>