delete的api函数
@post('/api/blogs/{id}/delete') async def api_delete_blog(request, *, id): check_admin(request) if not id or not id.strip(): raise APIValueError('id', 'id cannot be empty.') blog = await Blog.find(id) await blog.remove() return {"result": "OK"}
edit的api函数
@get('/manage/blogs/edit')
def manage_edit_blog(request,*,id):
check_admin(request)
return {
'__template__': 'manage_blog_edit.html',
'id': id,
'action': '/api/upblog'
}
@post('/api/upblog')
async def api_update_blog(request, *, name, summary, content,blog_id):
if not blog_id or not blog_id.strip():
raise APIValueError('blog_id','lose blog_id')
if not name or not name.strip():
raise APIValueError('name','name cannot be empty.')
if not summary or not summary.strip():
raise APIValueError('summary', 'summary cannot be empty.')
if not content or not content.strip():
raise APIValueError('content', 'content cannot be empty.')
blog=await Blog.find(blog_id)
if not blog:
blog = Blog(user_id=request.__user__.id, user_name=request.__user__.name, user_image=request.__user__.image, name=name.strip(), summary=summary.strip(), content=content.strip())
await blog.save()
else:
blog.name=name
blog.summary=summary
blog.content=content
await blog.update()
return blog
然后还要在manage_blog_edit.html里面的js稍作修改让他支持edit,修改的地方备注了
function initVM(blog) { var vm = new Vue({ el: '#vm', data: blog, methods: { submit: function (event) { event.preventDefault(); var $form = $('#vm').find('form'); params=this.$data if(ID){params.blog_id=ID} //多传一个blog_id判断是修改还是第一次编辑 $form.postJSON(action, params, function (err, r) { if (err) { $form.showFormError(err); } else { return location.assign('/api/blogs/' + r.id); } }); } } });
Sign in to make a reply
落魄的毛哥
delete的api函数
edit的api函数
@get('/manage/blogs/edit')
def manage_edit_blog(request,*,id):
check_admin(request)
return {
'__template__': 'manage_blog_edit.html',
'id': id,
'action': '/api/upblog'
}
@post('/api/upblog')
async def api_update_blog(request, *, name, summary, content,blog_id):
check_admin(request)
if not blog_id or not blog_id.strip():
raise APIValueError('blog_id','lose blog_id')
if not name or not name.strip():
raise APIValueError('name','name cannot be empty.')
if not summary or not summary.strip():
raise APIValueError('summary', 'summary cannot be empty.')
if not content or not content.strip():
raise APIValueError('content', 'content cannot be empty.')
blog=await Blog.find(blog_id)
if not blog:
blog = Blog(user_id=request.__user__.id, user_name=request.__user__.name, user_image=request.__user__.image, name=name.strip(), summary=summary.strip(), content=content.strip())
await blog.save()
else:
blog.name=name
blog.summary=summary
blog.content=content
await blog.update()
return blog
然后还要在manage_blog_edit.html里面的js稍作修改让他支持edit,修改的地方备注了