Flask路由注册异常问题排查求助
Flask路由注册异常问题排查求助
我现在正在开发一个Flask Web应用,遇到了一些意料之外的问题。
我的项目结构大致如下:
app/__init__.py
def create_app(test_config=None): .. some other stuff ... from app.views import auth app.register_blueprint(auth) return app
app\views\__init__.py
from flask import Blueprint auth = Blueprint('auth', __name__, url_prefix='/auth') from .auth_views import auth as auth_bp def register_blueprints(app): app.register_blueprint(auth_bp)
app\views\auth_views.py
from flask import Blueprint from app.controllers import auth_controller auth = Blueprint('auth', __name__) #register route @auth.route('/register', methods=['GET', 'POST']) def register(): return auth_controller.register() ..... some other routes ....
app\controllers\auth_controller.py
from flask import render_template, request, redirect, url_for, flash, abort, Blueprint from flask_login import login_user, logout_user, current_user from app import db, bcrypt from app.models import User,Role from app.forms.auth_forms import RegistrationForm, LoginForm, ForgotPasswordForm, ResetPasswordForm from app.utils import send_reset_email, user_has_capability from itsdangerous import URLSafeTimedSerializer from werkzeug.security import generate_password_hash, check_password_hash import os # IMport at last to avoid circular imports from app.views import auth @auth.route('/register', methods=['GET', 'POST']) def register(): form = RegistrationForm() if form.validate_on_submit(): user = User(username=form.username.data, email=form.email.data) hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8') user.password_hash = hashed_password # Assign default role 'contributor' to the new user author_role = Role.query.filter_by(name='contributor').first() if author_role: user.roles.append(author_role) db.session.add(user) db.session.commit() flash('Registration successful. Please log in.', 'success') return redirect(url_for('auth.login')) return render_template('register.html', form=form)
现在我遇到的问题是:
- 如果我把
auth_controller里register方法上的@auth.route('/register', methods=['GET', 'POST'])装饰器去掉,就会一直收到这个错误:
Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again
- 如果
auth_controller里的register方法保留这个装饰器,我能访问到注册表单,但只能通过127.0.0.1:5000/auth/register访问,而不是127.0.0.1:5000/register(我并没有给这个路由定义url_prefix啊)
我到底哪里做错了?该怎么解决这个问题?
备注:内容来源于stack exchange,提问作者Ritesh Rijal
相关产品推荐
相关产品推荐

