182-beego简单文章管理

30 篇文章 1 订阅



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)
依然还是会添加字段的
不用改





 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值