23-----BBS論壇
阿新 • • 發佈:2018-11-26
https llb tex wrapper quest http mac apps base
BBS論壇(二十三)
23.添加板塊
(1)apps/models
class BoardModel(db.Model): __tablename__ = ‘board‘ id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(20), nullable=False) create_time = db.Column(db.DateTime, default=datetime.now)
(2)manage.py
from apps.models import BoardModel
生成數據庫表
python manage.py db migrate
python manage.py db upgrade
(3)cms/forms.py
class AddBoardsForm(BaseForm): name=StringField(validators=[InputRequired(message=‘請輸入版塊名稱‘),Length(2,15,message=‘長度應在2-15個字符之間‘)]) class UpdateBoardForm(AddBoardsForm): board_id=IntegerField(validators=[InputRequired(message=‘請輸入版塊名稱‘)])
(4)cms/views.py
@bp.route(‘/boards/‘) @login_required @permission_required(CMSPermission.BOARDER) def boards(): board_models=BoardModel.query.all() context={ ‘boards‘:board_models } return render_template(‘cms/cms_boards.html‘,**context) @bp.route(‘/aboards/‘,methods=[‘POST‘]) @login_required @permission_required(CMSPermission.BOARDER) def aboards(): form=AddBoardsForm(request.form) if form.validate(): name=form.name.data board=BoardModel(name=name) db.session.add(board) db.session.commit() return restful.success() else: return restful.params_error(message=form.get_error()) @bp.route(‘/uboards/‘,methods=[‘POST‘]) @login_required @permission_required(CMSPermission.BOARDER) def uboards(): form=UpdateBoardForm(request.form) if form.validate(): board_id=form.board_id.data name=form.name.data board=BoardModel.query.get(board_id) if board: board.name=name db.session.commit() return restful.success() else: return restful.params_error(message=‘沒有這個版塊‘) else: return restful.params_error(message=form.get_error()) @bp.route(‘/dboards/‘,methods=[‘POST‘]) @login_required @permission_required(CMSPermission.BOARDER) def dboards(): board_id=request.form.get(‘board_id‘) if not board_id: return restful.params_error(message=‘請傳入版塊ID‘) board=BoardModel.query.get(board_id) if board: db.session.delete(board) db.session.commit() return restful.success() else: return restful.params_error(message=‘沒有這個版塊‘)
(5)cms/js/boards.js
$(function () { $(‘#add_board_btn‘).on(‘click‘, function () { event.preventDefault(); zlalert.alertOneInput({ ‘title‘:‘添加板塊‘, ‘text‘: ‘請輸入板塊名稱‘, ‘placeholder‘: ‘版塊名稱‘, ‘confirmCallback‘: function (inputValue) { zlajax.post({ ‘url‘: ‘/cms/aboards/‘, ‘data‘: { ‘name‘: inputValue }, ‘success‘: function (data) { if (data[‘code‘] == 200) { window.location.reload(); } else { zlalert.alertInfo(data[‘message‘]); } } }); } }) }); });
(6)cms/cms_boards.html
{% extends ‘cms/cms_base.html‘ %} {% from‘common/_macros.html‘ import static %} {% block title %} 板塊管理 {% endblock %} {% block head %} <script src="{{ static(‘cms/js/boards.js‘) }}"></script> {% endblock %} {% block page_title %} {{ self.title() }} {% endblock %} {% block main_content %} <div class="top-box"> <button class="btn btn-warning" id="add_board_btn" style="float: right">添加新版塊</button> </div> <table class="table table-bordered"> <thead> <tr> <th>版塊名稱</th> <th>帖子數量</th> <th>創建時間</th> <th>操作</th> </tr> </thead> <tbody> {% for board in boards %} <tr data-name="{{ board.name }}" data-id="{{ board.id }}"> <td>{{ board.name }}</td> <td>0</td> <td>{{ board.create_time }}</td> <td> <button class="btn btn-default edit-board-btn">編輯</button> <button class="btn btn-danger delete-board-btn">刪除</button> </td> </tr> {% endfor %} </tbody> </table> {% endblock %}
- 23.添加板塊
23-----BBS論壇