如何在蓝本中使用subdomain?


#1

auth.py中源码:

auth_bp = Blueprint('auth', __name__, subdomain='auth')


@auth_bp.route('/login', methods=['GET', 'POST'], subdomain='auth')
def login():
    if current_user.is_authenticated:
        return redirect(url_for('blog.index'))

    form = LoginForm()
    if form.validate_on_submit():
        username = form.username.data
        password = form.password.data
        remember = form.remember.data
        admin = Admin.query.first()
        if admin:
            if username == admin.username and admin.validate_password(password):
                login_user(admin, remember)
                flash('Welcome back.', 'info')
                return redirect_back()
            flash('Invalid username or password.', 'warning')
        else:
            flash('No account.', 'warning')
    return render_template('auth/login.html', form=form)


@auth_bp.route('/logout', subdomain='auth')
@login_required
def logout():
    logout_user()
    flash('Logout success.', 'info')
    return redirect_back()

init.py源码:

def register_blueprints(app):  # 注册蓝本
    app.register_blueprint(blog_bp)                         # 注册蓝本
    app.register_blueprint(admin_bp, url_prefix='/admin')   # 注册蓝本
    # app.register_blueprint(auth_bp, url_prefix='/auth')
    app.register_blueprint(auth_bp, subdomain='auth')

模板中源码:

                    {% if current_user.is_authenticated %}
                        <a href="{{ url_for('auth.logout', next=request.full_path)}}">Logout</a>
                    {% else %}
                        <a href="{{ url_for('auth.login', next=request.full_path) }}">Login</a>
                    {% endif %}

但点击后显示网页找不到,请问该如何改正?


#2

问题描述不清楚,没看懂。如果想添加子域支持的话,可以在第 10 章找到相关内容。


#3

好的,看好第十章后,不懂再问!谢谢