Flask 蓝图路由配置错误修复:如何正确定义路由和重定向
@blue.route('/' ,methods=[ 'POST' , 'GET'])#可以是post、get请求 def index(): return render_template('zhuce.html')
@blue.route('/zhuce', methods=['POST', 'GET']) # 添加装饰器定义路由 def zhuce():
# post请求
username=request.form.get('username')
pwd=request.form.get('password')
age=request.form.get('age')
sex=request.form.get('sex')
aihao=request.form.getlist('aihao')
print(username, ' ====',pwd,'=====', age, ' ====',sex)
print('爱好',aihao)
print(request.method)
print(request.args)
print(request.values)
print(request.host_url)
print(request.full_path)
return redirect(url_for('blue.zhuce')) # 重定向到zhuce函数
@blue.route('/demo') def demo(): print('从注册页面过来') return '这是demo'
错误分析
根据提供的代码,存在一个错误。在zhuce函数中,缺少了装饰器@blue.route('/zhuce')来定义路由,因此无法通过URL访问该函数。
解决方案
- 添加装饰器@blue.route('/zhuce')到zhuce函数,以定义其路由。
- 将index函数的返回值改为重定向到zhuce函数,即return redirect(url_for('blue.zhuce'))。
- 使用@blue.route('/demo')来定义demo函数的路由。
代码修改示例
@blue.route('/' ,methods=[ 'POST' , 'GET'])#可以是post、get请求
def index():
return render_template('zhuce.html')
@blue.route('/zhuce', methods=['POST', 'GET']) # 添加装饰器定义路由
def zhuce():
# post请求
username=request.form.get('username')
pwd=request.form.get('password')
age=request.form.get('age')
sex=request.form.get('sex')
aihao=request.form.getlist('aihao')
print(username, ' ====',pwd,'=====', age, ' ====',sex)
print('爱好',aihao)
print(request.method)
print(request.args)
print(request.values)
print(request.host_url)
print(request.full_path)
return redirect(url_for('blue.zhuce')) # 重定向到zhuce函数
@blue.route('/demo')
def demo():
print('从注册页面过来')
return '这是demo'
完成以上修改后,代码应该能够正常运行。
原文地址: https://www.cveoy.top/t/topic/o9e2 著作权归作者所有。请勿转载和采集!