beego简单文章管理
type Article struct{
Id int `orm:"pk;auto"`
Title string `orm:"size(100)"`
Time time.Time `orm:"type(datetime)"`
Count int `orm:"default(0)"`
Type string `orm:"size(50)"`
Img string `orm:"null"`
price float64 `orm:"digits(10);decimals(2)"`
}
比如这样一个小数
1234.56789
digits:9 总位数
decimals:5 小数位数
比如User这样的表
UserName字段是唯一的
那么要用unique
type User struct{
Id int
UserName string `orm:"unique"`
}
比如一些文件会有时间
创建时间:
auto_now
更新时间:
auto_now_add
创建好表了,创建了一张article表
Time time.Time `orm:""`
修改一下a标签
<a href="/add">添加文章</a>
beego.Router("/add",&controllers.ArticleController{},"get:ShowAdd")
然后在controllers
创建article.go文件
func (this *ArticleController) ShowAdd(){
this.TplName="add.html"
}
这样的话,点击添加文章
跳转到add.html页面
现在正式添加文章
func (this *ArticleController) HandleAdd(){
title := this.GetString("articleName")
content := this.GetString("content")
file,head,err := this.GetFile("uploadname")
defer file.Close( )
//校验数据
if title == "" || content== "" || err!=nil{
this.Data["errmsg"]="添加文章失败"
this.TplName="add.html"
return
}
//beego.Info(file,head)
//把图片存起来
this.SaveToFile("uploadname","./static/img/"+head.Filename)
//处理数据
o := orm.NewOrm()
var article models.Article
article.Title=title
article.Content=content
article.Img="/static/img/"+head.Filename
//插入数据库
o.Insert(&article)
//返回数据
this.Redirect("/index",302)
}
注意,上传文件
form里面加上
enctype="multipart/form-data"
<form method="post" action="/add" enctype="multipart/form-data">
注意,给表添加字段
这样的话
orm.RunSyncdb("",false,true)
依然还是会添加字段的
不用改