AssertionError: View function mapping is overwriting an existing endpoint function: home - flask
。
我正在努力学习http://code.tutsplus.com/tutorials/an-introduction-to-pythons-flask-framework--net-28822
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | C:\envs\virtalenvs\flask_mini\Scripts\python.exe C:/envs/r2/mini2/intro_to_flask/__init__.py C:\envs\virtalenvs\flask_mini\lib\site-packages\flask_sqlalchemy\__init__.py:800: UserWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True to suppress this warning. warnings.warn('SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True to suppress this warning.') Traceback (most recent call last): File"C:/envs/r2/mini2/intro_to_flask/__init__.py", line 13, in <module> from routes import mail File"C:\envs 2\mini2\intro_to_flask outes.py", line 9, in <module> @app.route('/') File"C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1013, in decorator self.add_url_rule(rule, endpoint, f, **options) File"C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 62, in wrapper_func return f(self, *args, **kwargs) File"C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 984, in add_url_rule 'existing endpoint function: %s' % endpoint) AssertionError: View function mapping is overwriting an existing endpoint function: home |
routes.py文件是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | from intro_to_flask import app from flask import render_template, request, flash, session, url_for, redirect from forms import ContactForm, SignupForm, SigninForm from flask.ext.mail import Message, Mail from models import db, User mail = Mail() @app.route('/') def home(): return render_template('home.html') @app.route('/about') def about(): return render_template('about.html') @app.route('/contact', methods=['GET', 'POST']) def contact(): form = ContactForm() if request.method == 'POST': if form.validate() == False: flash('All fields are required.') return render_template('contact.html', form=form) else: msg = Message(form.subject.data, sender='[email protected]', recipients=['[email protected]']) msg.body =""" From: %s <%s> %s """ % (form.name.data, form.email.data, form.message.data) mail.send(msg) return render_template('contact.html', success=True) elif request.method == 'GET': return render_template('contact.html', form=form) @app.route('/signup', methods=['GET', 'POST']) def signup(): form = SignupForm() if 'email' in session: return redirect(url_for('profile')) if request.method == 'POST': if form.validate() == False: return render_template('signup.html', form=form) else: newuser = User(form.firstname.data, form.lastname.data, form.email.data, form.password.data) db.session.add(newuser) db.session.commit() session['email'] = newuser.email return redirect(url_for('profile')) elif request.method == 'GET': return render_template('signup.html', form=form) @app.route('/profile') def profile(): if 'email' not in session: return redirect(url_for('signin')) user = User.query.filter_by(email=session['email']).first() if user is None: return redirect(url_for('signin')) else: return render_template('profile.html') @app.route('/signin', methods=['GET', 'POST']) def signin(): form = SigninForm() if 'email' in session: return redirect(url_for('profile')) if request.method == 'POST': if form.validate() == False: return render_template('signin.html', form=form) else: session['email'] = form.email.data return redirect(url_for('profile')) elif request.method == 'GET': return render_template('signin.html', form=form) @app.route('/signout') def signout(): if 'email' not in session: return redirect(url_for('signin')) session.pop('email', None) return redirect(url_for('home')) |
号
这可能是什么原因造成的?
编辑:
当我把about函数放在第一位时,我得到:
1 | AssertionError: View function mapping is overwriting an existing endpoint function: about |
因此,我认为问题可能出在import语句中,或者至少在第一个路由之前:
1 2 3 4 5 6 7 | from intro_to_flask import app from flask import render_template, request, flash, session, url_for, redirect from forms import ContactForm, SignupForm, SigninForm from flask.ext.mail import Mail, Message from models import db, User mail = Mail() |
。
编辑2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | from flask import Flask app = Flask(__name__) app.secret_key = 'development key' app.config["MAIL_SERVER"] ="smtp.gmail.com" app.config["MAIL_PORT"] = 465 app.config["MAIL_USE_SSL"] = True app.config["MAIL_USERNAME"] = '[email protected]' app.config["MAIL_PASSWORD"] = 'your-password' from routes import mail mail.init_app(app) app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://your-username:your-password@localhost/development' from models import db db.init_app(app) import intro_to_flask.routes |
此错误是因为您是循环导入应用程序(在routes.py中导入应用程序,在app中导入routes.py)这种模式不起作用,也不正确。在flask中,您可以将整个应用程序编写成单个文件,也可以使用flask蓝图以模块化方式制作。http://flask.pocoo.org/docs/0.10/蓝图/
应用程序可以在单个文件中工作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | from flask import render_template, request, flash, session, url_for, redirect from forms import ContactForm, SignupForm, SigninForm from flask.ext.mail import Message, Mail from models import db, User from flask import Flask app = Flask(__name__) app.secret_key = 'development key' app.config["MAIL_SERVER"] ="smtp.gmail.com" app.config["MAIL_PORT"] = 465 app.config["MAIL_USE_SSL"] = True app.config["MAIL_USERNAME"] = '[email protected]' app.config["MAIL_PASSWORD"] = 'your-password' mail = Mail() mail.init_app(app) app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://your-username:your-password@localhost/development' from models import db db.init_app(app) @app.route('/') def home(): return render_template('home.html') @app.route('/about') def about(): return render_template('about.html') @app.route('/contact', methods=['GET', 'POST']) def contact(): form = ContactForm() if request.method == 'POST': if form.validate() == False: flash('All fields are required.') return render_template('contact.html', form=form) else: msg = Message(form.subject.data, sender='[email protected]', recipients=['[email protected]']) msg.body =""" From: %s <%s> %s """ % (form.name.data, form.email.data, form.message.data) mail.send(msg) return render_template('contact.html', success=True) elif request.method == 'GET': return render_template('contact.html', form=form) @app.route('/signup', methods=['GET', 'POST']) def signup(): form = SignupForm() if 'email' in session: return redirect(url_for('profile')) if request.method == 'POST': if form.validate() == False: return render_template('signup.html', form=form) else: newuser = User(form.firstname.data, form.lastname.data, form.email.data, form.password.data) db.session.add(newuser) db.session.commit() session['email'] = newuser.email return redirect(url_for('profile')) elif request.method == 'GET': return render_template('signup.html', form=form) @app.route('/profile') def profile(): if 'email' not in session: return redirect(url_for('signin')) user = User.query.filter_by(email=session['email']).first() if user is None: return redirect(url_for('signin')) else: return render_template('profile.html') @app.route('/signin', methods=['GET', 'POST']) def signin(): form = SigninForm() if 'email' in session: return redirect(url_for('profile')) if request.method == 'POST': if form.validate() == False: return render_template('signin.html', form=form) else: session['email'] = form.email.data return redirect(url_for('profile')) elif request.method == 'GET': return render_template('signin.html', form=form) @app.route('/signout') def signout(): if 'email' not in session: return redirect(url_for('signin')) session.pop('email', None) return redirect(url_for('home')) app.run() |