返回顶部
扩大
缩小

Zhang_derek

BBS论坛(二十六)

26.发布帖子前台代码逻辑完成

(1)front/hooks.py

from .views import bp
from flask import session,g
from .models import FrontUser
import config

@bp.before_request
def before_request():
    if config.FRONT_USER_ID in session:
        user_id=session.get(config.FRONT_USER_ID)
        user=FrontUser.query.get(user_id)
        if user:
            g.front_user=user

(2)front/init.py

from . import hooks

(3)front/front_base.html

 <ul class="nav navbar-nav navbar-right">
                {% if g.front_user %}
                    <li class="dropdown">
                        <a class="dropdown-toggle" id="dropdownMenu1" data-toggle="dropdown"
                           aria-haspopup="true" aria-expanded="true">
                            {{ g.front_user.username }}
                            <span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
                            <li><a href="#">个人中心</a></li>
                            <li><a href="#">设置</a></li>
                            <li><a href="#">退出登录</a></li>
                        </ul>
                    </li>
                {% else %}
                    <li><a href="{{ url_for('front.signin') }}">登录</a></li>
                    <li><a href="{{ url_for('front.signup') }}">注册</a></li>
                {% endif %}
            </ul>

(4)front/apost.js

$(function () {
    var ue = UE.getEditor('ueditor', {'serverUrl': '/ueditor/upload/'});
    $('#submit-btn').on('click', function (event) {
        event.preventDefault();
        var titleInput = $('input[name=title]');
        var boardSelect = $('select[name=board_id]');

        var title = titleInput.val();
        var board_id = boardSelect.val();
        var content = ue.getContent();
        zlajax.post({
            'url': '/apost/',
            'data': {
                'title': title,
                'board_id': board_id,
                'content': content
            },
            'success': function (data) {
                if (data['code'] == 200) {
                    zlalert.alertConfirm({
                        'msg': '帖子发表成功',
                        'cancelText': '返回首页',
                        'confirmText': '再写一篇',
                        'cancelCallback': function () {
                            window.location = '/';
                        },
                        'confirmCallback': function () {
                            titleInput.val('');
                            ue.setContent('');

                        }
                    });
                } else {
                    zlalert.alertInfo(data['message']);
                }

            }
        })
    });
});

(5)config.py

UEDITOR_UPLOAD_PATH=os.path.join(os.path.dirname(__file__),'images')

 

posted on 2018-06-10 16:20  zhang_derek  阅读(...)  评论(... 编辑 收藏

导航