《Flask Web开发》学习笔记之bug--(2)【AssertionError: View function mapping is overwriting an existing endpoi】

这篇博客记录了在学习《Flask Web开发》过程中遇到的一个bug,即AssertionError提示视图函数映射重复。问题根源在于hello.py文件中,@app.route('/')装饰器下存在两个index()函数,导致冲突。解决方案是删除冗余的index()函数,从而消除错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# hello.py

from datetime import datetime
from flask import Flask, render_template
from flask_script import Manager
from flask_bootstrap import Bootstrap
from flask_moment import Moment


app = Flask(__name__)

manager = Manager(app)
bootstrap = Bootstrap(app)
moment = Moment(app)

@app.errorhandler(404)
def page_not_found(e):
	return render_template('404.html'), 404

@app.errorhandler(500)
def internal_server_error(e):
	return render_template('500.html'), 500

@app.route('/')
def index():
	return render_template('index.html', current_time=datetime.utnow())

@app.route('/')
def index():
	return render_template('index.html')

@app.route('/user/<name>')
def user(name):
	return render_template('user.html', name=name)


if __name__ == '__main__':
	manager.run()

$ python3 hello.py runserver --host 0.0.0.0
Traceback (most recent call last):
  File "hello.py", line 26, in <module>
    @app.route('/')
  File "/home/henry/.local/lib/python3.5/site-packages/flask/app.py", line 1080, in decorator
    self.add_url_rule(rule, endpoint, f, **options)
  File "/home/henry/.local/lib/python3.5/site-packages/flask/app.py", line 64, in wrapper_func
    return f(self, *args, **kwargs)
  File "/home/henry/.local/lib/python3.5/site-packages/flask/app.py", line 1051, in add_url_rule
    'existing endpoint function: %s' % endpoint)
AssertionError: View function mapping is overwriting an existing endpoint function: index

定位:

hello.py的@app.route('/')装饰器重写了index函数

解决办法:

删掉一个多余的index()函数。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值