BBS Forum (26)

BBS Forum (26)

26. Post the front desk code logic is completed

(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="#">Personal Center</a></li>
                            <li><a href="#">Settings</a></li>
                            <li><a href="#">Sign out</a></li>
                        </ul>
                    </li>
                {% else %}
                    <li><a href="{{ url_for('front.signin') }}">Sign in</a></li>
                    <li><a href="{{ url_for('front.signup') }}">Sign up</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':'Post successfully published',
                        'cancelText':'Return to homepage',
                        'confirmText':'Write another article',
                        'cancelCallback': function () {
                            win.loc ='/';
                        },
                        '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')
Reference: https://cloud.tencent.com/developer/article/1390474 BBS Forum (26)-Cloud + Community-Tencent Cloud