BBS论坛(二十三)
2025-01-16 05:54:45

LIKE.TG 成立于2020年,总部位于马来西亚,是首家汇集全球互联网产品,提供一站式软件产品解决方案的综合性品牌。唯一官方网站:www.like.tg
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 %}

现在关注【LIKE.TG出海指南频道】、【LIKE.TG生态链-全球资源互联社区】,即可免费领取【WhatsApp、LINE、Telegram、Twitter、ZALO云控】等获客工具试用、【住宅IP、号段筛选】等免费资源,机会难得,快来解锁更多资源,助力您的业务飞速成长!点击【联系客服】
本文由LIKE.TG编辑部转载自互联网并编辑,如有侵权影响,请联系官方客服,将为您妥善处理。
This article is republished from public internet and edited by the LIKE.TG editorial department. If there is any infringement, please contact our official customer service for proper handling.